Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Else statement
#1
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?
Reply
#2
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you soo much ichabod801 i tried both workaround that you gave and it works now :)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020