How to use if, Elif and Else

Asked

Viewed 307 times

2

GOTHAM CITY Challenge (level 1)

You are a police officer and need to report a criminal activity and on report you will give a number from 0 to 10 regarding the level of the crime.

Make a code that receives by the user a number from 0 to 10,

if the number is less than 5, print on the screen "Police can resolve!". If the number is between 5 and 8, print on the screen "We need the help from Batman!". If the number is greater than 8, print on the screen "Leave that Batman can handle on his own!".

Annotations:

  1. so that the user can enter a value, use the function input()
  2. so that you can check more than one if condition, use the elif which in Python means else if.

My code:

n = int(input('De 0 a 10, qual a gravidade do crime?: '))
if n < 5:
    print('Os policiais podem resolver')
elif n >= 5:
    print('Os policiais precisam da ajuda do Batman!')
elif n <= 8:
    print('Os policiais precisam da ajuda do Batman!')
else:
    print('O Batman resolve sozinho!')

Where is the error in my code? I employed some wrong use of some condition?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

3 answers

6

The statement says exactly what you should write in the code, and it’s not written, there are 3 conditions set and you put 4, even repeat the same action for two different conditions:

n = int(input('De 0 a 10, qual a gravidade do crime?: '))
if n < 5: #se o numero for menor que 5
    print('Os policiais podem resolver')
elif 5 <= n <= 8: #se o numero for entre 5 a 8
    print('Os policiais precisam da ajuda do Batman!')
elif n > 8: #se o numero for maior que 8
    print('O Batman resolve sozinho!')

The statement is not clear with what to do if the person enters invalid values, but I assumed the same as you, if you type negative accepted as if it were less than 5 or if you type more than 10 assumes that it is greater than 8.

If you want to simplify, but this is a little more advanced because it is not obvious that it is to do what the statement says, but it is still pure logic:

n = int(input('De 0 a 10, qual a gravidade do crime?: '))
if n < 5: #se o numero for menor que 5
    print('Os policiais podem resolver')
elif n <= 8: #se o numero for menor ou igual a 8, já se sabe que não é menor que 5
    print('Os policiais precisam da ajuda do Batman!')
else: #já se sabe que o número não é menor ou igual a 8, então é maior
    print('O Batman resolve sozinho!')

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Why can you do that? Math only. If you’re less than 5 you fall in the first if and no longer executes others. If it is not less than 5 can fall on others, but it is already certain that it is 5 or more, do not need to test it again. Just need to ensure that the ceiling is 8. And finally the last will only run if it is higher 8, the previous conditions guarantee this, no need by any condition.

I only fall in one else when the ifprevious s gave false, it is a form of short circuit, when it gives true closes the circuit and does not continue.

0

Hello,

You can make the following conditional structure:

n = int(input('De 0 a 10, qual a gravidade do crime?: '))

if n < 5:
    print('Os policiais podem resolver')
elif n >= 5 and n <= 8:
    print('Os policiais precisam da ajuda do Batman!')
else:
    print('O Batman resolve sozinho!')
  • aaaa understood, so basically it was just put the and there...I had thought to leave in only three conditions but n knew how to leave between 5 and 8 ended up creating a condition the more q made the program get weird... thanks!

  • 2

    In this case the and is redundant. To test range in Python just this: 5 <= n <= 8 (which in that case is not necessary, the first if already eliminates everything that is less than 5)

0

Validating input values from 0 to 10 and proposed if / Elif / Else.

num_crime = -1
while not int(num_crime) in range(0,11):
    num_crime = input('Informe a gravidade do crime (0 - 10) : ')

if int(num_crime) < 5:
    print('Os policiais podem resolver')
elif int(num_crime) <= 8:
    print('Os policiais precisam da ajuda do Batman!')
else:
    print('O Batman resolve sozinho!')

Abs,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.