IF, ELIF and ELSE

2

I'm taking my first steps in Python, and I had a question in the if / else statement, trying to solve the following exercise:

"Based on input data for a person's height and gender, 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 the person's weight and tell them if they are in, above or below weight.

This was the logic I used:

``````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

Could you help me?

Thank you!

asked by anonymous 04.10.2016 / 19:54

2

When you put `sexo == H` it understands that you are referring to the `H` variable. Enclose in quotation marks, like this: `sexo == "H" or sexo == "h"`

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

I do not 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()
``````

04.10.2016 / 20:15
-1

In addition to correcting the indentation and quotation marks, you should use the `raw_input()` function rather than the simple `input()` function. When using the `input()` function, the user should write "h" (with quotation marks) so that the program recognizes the character as a string.

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

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

04.10.2016 / 20:36