Beginner in Python, else, and elif

4
idade = int(input("Insira sua idade:")) 
if(idade<=0): 
  print("Sua idade nao pode ser 0 ou menos de zero") 
elif(idade>150): 
   print("sua idade nao pode ser maior de 150 anos") 
elif(idade<18):
   print("voce precisa ter mais de 18 anos")

Why can not I use elif at all? Example: elif(idade<=0):

    
asked by anonymous 18.01.2017 / 22:14

5 answers

5

You need to understand the flow of things.

if

Executes a block of code if condition is met.

elif

This is an abbreviation for else if . That is, senão, e se or caso contrário, e se . It executes a particular block of code only if the condition of the if (or elif ) previous to it is not met and its condition is.

else

Executes a particular block of code if all previous conditions are not met.

Translating your code into a pseudo-language in Portuguese

se(idade <= 0)
    print("Sua idade nao pode ser 0 ou menos de zero") 
caso contrário, e se (idade > 150)
    print("sua idade nao pode ser maior de 150 anos") 
caso contrário, e se (idade < 18)
    print("voce precisa ter mais de 18 anos")
caso contrário
    print('Nenhuma condição atendida')
    
18.01.2017 / 22:29
2

The first condition will always be an IF called at the beginning of the conditional structure, right after it you can add as many ELIF as needed.

I recommend you take a look at conditional structures in some programming logic book.

Here is a link to get a better understanding of this part: link

    
18.01.2017 / 22:30
2

Dude, if I understand you're in doubt about the if / else control blocks. Theoretically, in almost all programming languages, you have to have an initial condition with if , the others with elif (in the case of python but in some others it is else if ) and the last one, if no other is answered, with else . That is,

Se algo (if): isso
Se não, se algo (elif): aquilo
Se não, se algo (elif): aquilo outro
Se nenhuma das anteriores (else): aquilo final

Well, this is the default, but nothing prevents you from putting only elif . As long as they can cover all cases, that's fine, but you're not programming by following the standards.

    
18.01.2017 / 22:38
2

You need an if to use an elif. understand as follows:

Your code in a non-Pythonic way would look like this:

idade = int(input("Insira sua idade:")) 
if(idade<=0): 
  print("Sua idade nao pode ser 0 ou menos de zero") 
else:
    if(idade>150): 
         print("sua idade nao pode ser maior de 150 anos") 
    else:
         if(idade<18):
              print("voce precisa ter mais de 18 anos")

What Python does is simplify this structure to wipe the code. Therefore, assuming that ELIF is a simplified form of an Else followed by If, there is no reason to use a face elif without first using an if.

    
19.01.2017 / 01:16
1

Just to complement the answer, the correct code would be:

idade = int(input("Insira sua idade:")) 
if idade<=0:
   print("Sua idade nao pode ser 0 ou menos de zero")
elif idade<18:
   print("voce precisa ter mais de 18 anos")
elif idade>150:
   print("sua idade nao pode ser maior de 150 anos") 
else:
   print("Perfeito")

A correct concept is to always have the conditions IF in order of exclusion, eg from the minor to the greater, from the major to the minor .. then we start by checking negative numbers, then smaller than 18 and then if they are greater than 150 and if not perfect.

Of course, always start a condition block ( if ) with IF , then you can use ELSE or ELSE IF (In the case of else if = ELIF python) This is a programming feature that all languages follow!

    
19.01.2017 / 12:47