def BMI_calculator(inches, weight):
""" This function takes a persons height in feet and inches and their
weight in pounds and calculates their BMI"""
# Now we can just convert and calculate BMI
metric_height= inches* .025
metric_weight= weight* .45
BMI = int(metric_weight/(metric_height)**2)
print BMI
return BMI
def BMI_user_calculator():
""" This function will ask the user their body information and calulate
the BMI with that info"""
# First we need to gather information from the user to put into the BMI calculator
user_weight = int(raw_input('Enter your weight in pounds: '))
user_height = int(raw_input('Enter your height in inches: '))
print "Youre BMI is",
BMI_calculator(user_height, user_weight)
If BMI < 18.5:
print "You're BMI is below the healthy range for your age"
elif BMI > 24.9:
print "You're BMI is above the healthy range for your age"
else:
print "You are in the healthy BMI range!"
这是我到目前为止的代码,但是在运行时,我在if
语句中收到语法错误,指出BMI
未定义。 BMI
从第一个函数返回,所以我真的不明白发生了什么
答案 0 :(得分:1)
您的BMI
中没有声明BMI_user_calculator()
,这就是为什么它没有定义BMI。在if-elif语句中使用BMI进行比较之前,您应首先声明BMI。
此外,您的If
应为if
。 Python区分大小写。
您的代码应该是这样的:
def BMI_calculator(inches, weight):
metric_height= inches* .025
metric_weight= weight* .45
BMI = int(metric_weight/(metric_height)**2)
# No need to print BMI here anymore since you're returning it anyway
# print BMI
return BMI
def BMI_user_calculator():
user_weight = int(raw_input('Enter your weight in pounds: '))
user_height = int(raw_input('Enter your height in inches: '))
BMI = BMI_calculator(user_height, user_weight)
print "Your BMI is", BMI
if BMI < 18.5:
print "Your BMI is below the healthy range for your age"
elif BMI > 24.9:
print "Your BMI is above the healthy range for your age"
else:
print "You are in the healthy BMI range!"
if __name__ == '__main__':
BMI_user_calculator()