【发布时间】:2017-11-02 09:13:43
【问题描述】:
我正在尝试创建一个程序来计算一个人的 BMI 作为我的作业。我在第 9 行收到“意外缩进”错误,即 ***print(“您的 BMI 是:”,BMI)。 我已经来回移动缩进以纠正它,但仍然无法正常工作。请帮忙,因为我只是一个初学者。提前谢谢你。
user_input = float(input("Let's calculate your BMI! Please select if you 'K' for kilograms and L for pounds for your weight: ")).upper
if user_input == "K":
user_input_weight_kgs = float(input("Weight in kgs.:"))
user_input_height = float(input("Height by inches: "))
user_input_age = (input("Age:"))
BMI_for_kgs = float(user_input_weight / (user_input_height **2))
print ("Your BMI is: ",BMI)
if BMI < 18.5 :
print ("Under Weight")
elif BMI < 26:
print ("Normal Weight")
else:
print ("Over Weight")
elif user_input == "L":
user_input_weight_lbs = float(input("Weight in lbs.: "))
user_input_height = float(input("Height by inches: "))
user_input_age = (input("Age:"))
BMI_for_lbs = float((user_input_weight * 703) / (user_input_height **2))
print ("Your BMI is: ",BMI)
if BMI < 18.5 :
print ("Under Weight")
elif BMI < 26:
print ("Normal Weight")
else:
print ("Over Weight")
elif guess.isnumeric():
print ("Please select an alphabet only! Letter 'K' or 'L'")
elif len(guess) > 1:
print ("Please choose a single alphabet only! Letter 'K' or 'L'")
elif len(guess) == 0:
print ("You need to enter a letter! Letter 'K' or 'L'")
else:
break
【问题讨论】:
-
因为这一行没有正确缩进 - 把它放回一个制表符!
-
您的代码甚至不会检查第一个
if user_input == "K",因为您正在将输入转换为float并且您正在调用.upper()方法。 -
这两行
print ("Your BMI is: ",BMI)都缩进太多了。它们应该与上面的线对齐。 -
就用这个
user_input = input("Let's calculate your BMI! Please select if you 'K' for kilograms and L for pounds for your weight: ").upper()。您只需要字母“K”或“L”,因此无法将字母转换为数字。 -
缩进在 Python 中很重要。你会得到它的窍门。以后密切关注“Traceback”底部的错误信息。它通常会将您指向错误所在的行或至少附近的一个。
标签: python python-3.x