Python Forum

Full Version: Else statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm new to python and i'm trying to code for the first. I'm trying to follow the Else statement following this youtube video:
https : //www  .youtube.com /watch?v=BS7ZcQ1BcE0&index=9&list=PLw02n0FEB3E2RDlD2cBULQjvXJ1K_jS1O

However, I ran into some error. Here is my code:

weight = int(input("Enter weight in KG: \n"))
height = int(input("Enter height in cm: \n"))
age = int(input("Enter age: \n"))
isMale = bool(input("Are you male?(y/n)"))

if isMale:
        bmr = 66.5 + (13.75 * weight) + (5 * height) - (6.755 * age)
else:
   bmr = 66.5 + (13.75 * weight) + (5 * height) - (6.755 * age)

bmr= round(bmr)
print(bmr)
The error happens after inserting y. Here's the error message:
Are you male?(y/n)y
Traceback (most recent call last):
 File "else.py", line 20, in <module>
   isMale = bool (input("Are you male?(y/n)"))
 File "<string>", line 1, in <module>
NameError: name 'y' is not defined

Any idea what went wrong?
It looks like you are trying to run it in Python 2.x. In Python 2.x, input tries to evaluate what is inputted before returning it. When it does that it treats y as a variable name, which is not defined.

To fix this, either used raw_input (which will just return a string), or run it in Python 3.x (where input is equivalent to the 2.x raw_input).
Thank you soo much ichabod801 i tried both workaround that you gave and it works now :)