Beginner in Python, Else and Elif

Asked

Viewed 1,463 times

5

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’t I use elif at all? Example: elif(idade<=0):

  • 1

    if is "if", elif is "or if". Without the if doesn’t have the elif.

5 answers

5

You need to understand the flow of things.

if

Runs a code block if a certain condition is met.

elif

Is an abbreviation for else if. That is to say, senão, e se or caso contrário, e se.
Executes a particular code block only if the condition of if (or elif) before he is not met and his own condition is.

else

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

Translating your code into a pseudo-language in English

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')

3

3

Man, if I understand you’re having doubts about the control blocks if/Else. 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, in case no other is met, with the else. That is to say,

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, that’s the pattern, but nothing stops you from putting it alone elif. As long as they can cover all the cases, it’s okay, but you’re not programming by the standards.

2

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

your code in a form NOT pythonic 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 dry the code. So, assuming that the ELIF is a simplified form of an Else followed by If, there is no reason to use a face Elif without having used, before, an if.

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, e.g., from the smallest to the largest, from the largest to the smallest.. then we start by checking negative numbers, then smaller than 18 and then larger than 150 and if it’s not perfect.

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

Browser other questions tagged

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