【发布时间】:2021-12-25 18:34:42
【问题描述】:
我已经制作了这个 BMI 计算器,并且我希望用户每次使用 BMI 计算器时输入“y”来计算底部的用户计数。我似乎无法让代码工作。有什么帮助吗?
user_continue = "y"
counter = 0
while user_continue == "y":
weight = float(input("What is your weight? (KG) "))
height = float(input("What is your height? (Metres) "))
#formula to convert weight and height to users bmi
bmi = weight/(height*height)
print("Your BMI is", bmi)
#indicators to state if user is either underwieght, overweight or normal
if bmi < 18:
print("It indicates you underweight.")
elif bmi >= 18 and bmi < 25:
print("It indicates you are within normal bounds.")
elif bmi >= 25:
print("It indicates you are overweight.")
user_continue = input("Add Another BMI? y/n: ")
# add counter
if user_continue != "y":
counter+=1
print(counter)
print("\t\tThank You for using BMI calculator by Joe Saju!")
print("\n\t\t\t\tPress ENTER to Exit.")
break
【问题讨论】:
-
您只在用户退出程序时递增计数器 (
user_continue != "y")。这意味着它永远不会是1以外的任何东西。为什么不在循环顶部增加计数并在循环后打印消息。