我正在尝试创建一个程序来计算一个人的BMI作为我的作业。我得到"意外的缩进"第9行中的错误是***打印("您的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
答案 0 :(得分:2)
问题是第9行(打印行)没有正确缩进,这是修复:
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
您应该只在代码块中缩进代码。缩进开始一个块,unindenting结束它。没有明确的大括号,括号或关键字来结束Python中的代码块。
修改强>
回答其他问题(在评论中):
您的问题是,您希望看到字符串“K”或“L”,但您正在尝试将该字符串转换为浮点数。这应该表明出现了问题。另外,您实际上并没有调用upper()
这是另一个问题(除非您的意图是调用user_input()
来获取字符串的大写值,我认为这不是必需的)。解决:
user_input = input("Let's calculate your BMI! Please select if you 'K' for kilograms and L for pounds for your weight: ").upper()