劳拉,你的问题都是关于压痕的。Python知道如果连续的代码行都有相同的缩进(与C或Java不同,在C或Java中,代码块由左括号和右括号分隔)。在
您的代码应该如下所示:while True:
print("BMI Calculator")
weight = float(input("\nPlease enter your weight in KG: "))
height = float(input("\nPlease enter your height in metres: "))
bmi = weight/(height*height)
if bmi <= 18.5:
print("Your BMI is", bmi,"which means you are underweight.")
elif bmi > 18.5 and bmi < 25:
print("Your BMI is: ", bmi, "which means you are normal")
elif bmi > 25 and bmi < 30:
print("Your BMI is: ", bmi, "which means you are overweight")
elif bmi > 30:
print("Your BMI is: ", bmi, "which means you are obese")
else:
print("There was an error with your input, Sorry.")
answer = input("Would you like to enter another? key y/n: ")
if answer not in ("y", "n"):
print("Invalid Input")
break
if answer == "y":
continue
else:
input("\nPress the enter key to exit")
break
在这段代码中,我更改了布尔测试和指令顺序:
^{pr2}$
到if answer not in ("y", "n"):
print("Invalid Input")
break
如果中断循环,则不会执行该循环中的以下代码行。另外,您所做的比较总是返回True,因为答案是在(“y”,“n”)。我还删除了最后一个循环,因为这样更有意义。在
至于第二个代码,函数start()所做的唯一事情就是print("\nBMI Calculator"),这也是因为缩进级别的原因。在
希望有帮助:)