IF, ELIF AND ELSE

Asked

Viewed 1,892 times

1

I’m taking my first steps in Python, and I came up with a question in the if/Else sentence, trying to solve the following exercise:

"Using a person’s height and gender as input data, construct an algorithm that calculates their ideal weight using the following formulas:

For men: (72.7*h) - 58

For women: (62.1*h) - 44.7 (h = height)

Ask for the person’s weight and tell them if they are in, overweight or underweight."

That was the logic used by me:

def main():

altura = float(input("Digite a altura do paciente: "))
sexo = input("Digite o sexo do paciente, H(Masculino) ou F(Feminino): ")


altura
if sexo == H or sexo == h:
    peso = (72.7 * altura) - 58
    print ("O peso ideal do paciente é: ",peso)
    
    elif sexo == F or sexo == f:
        peso = (62.1 * altura) - 44.7
        print ("O peso ideal da paciente é: ",peso)
        
        else:
            print ("Sexo inválido")


pesopaciente = input("Digite o seu peso: ")

if pesopaciente < peso:
    print("Você está abaixo do peso ideal.")
    
    elif pesopaciente > peso:
        print("Você está acima do peso ideal.")
        
        else:
        print("Você está na média de peso.")
        

main()    

However, it returns the following error:

Traceback (Most recent call last):

File "python", line 12

Elif sex == F or sex == f:

^

Syntaxerror: invalid syntax

You could help me?

Grateful!

2 answers

2


When you put sexo == H he understands that you are referring to the variable H. Put it in quotes like this: sexo == "H" or sexo == "h"

Also the indentation is all wrong. The content of the function main is not indented, and the elif and the else must be in the same column as the if.

Read this article:

Indentation in python

I don’t have python 3 with me to test, only 2.7... but I believe this code will work:

def main():
    altura = float(input("Digite a altura do paciente: "))
    sexo = input("Digite o sexo do paciente, H(Masculino) ou F(Feminino): ")

    if sexo == "H" or sexo == "h":
        peso = (72.7 * altura) - 58
        print ("O peso ideal do paciente é: ",peso)
    elif sexo == "F" or sexo == "f":
        peso = (62.1 * altura) - 44.7
        print ("O peso ideal da paciente é: ",peso)
    else:
        print ("Sexo inválido")

    pesopaciente = input("Digite o seu peso: ")

    if pesopaciente < peso:
        print("Você está abaixo do peso ideal.")
    elif pesopaciente > peso:
        print("Você está acima do peso ideal.")
    else:
        print("Você está na média de peso.")

main()
  • Actually, my indentation was completely wrong. I’m kind of used to Java, I had no notion that it needed to be this way. If you can take me another question, as soon as I applied the corrections you gave me, I noticed that it is also necessary to give a tab on the personal line = input("Type your weight: "), would you know me why? Thank you very much, Filipe!

  • 1

    Why is he inside the main. If you took it out of indentation, both the line that you have says pesopaciente = input("Digite o seu peso: ") as the rest of the if would be out of the main, which is not what you want, I believe...

-1

In addition to correcting indentation and quotation marks, you should use the function raw_input(), instead of function input() simple. When using the function input(), the user must type "h" (with the quotes) so that the program recognizes the character as a string.

The function input() recognizes the user input as Python code. That is, by inserting only h (without quotation marks), python would try to evaluate this variable, which does not exist in your program.

The raw_input() function, in turn, returns the sequence of ASCII characters entered by the user before pressing ENTER, this string being interpreted as a string.

  • Dude, he’s using python 3, which he doesn’t have raw_input, only input. If your reply is derived from mine could have written as comment in my reply.

Browser other questions tagged

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