我制作了這個 BMI 計算器,我希望每次用戶通過輸入“y”使用 BMI 計算器時,底部的用戶計數。我似乎無法讓代碼作業。有什么幫助嗎?
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
uj5u.com熱心網友回復:
您想在回圈的任何迭代中增加計數器,因此您需要增加counter回圈內的變數,而不是結束 if 陳述句中的變數。
提示:在回圈開始時增加計數器變數(如下面的代碼所示)
在您的情況下,僅當用戶想要退出時計數器才會增加。所以它只會反擊一次。
user_continue = "y"
counter = 0
while user_continue == "y":
# increase the counter at the begining
counter =1
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: ")
if user_continue != "y":
# counter =1 line removed and moved to the begining
print(counter)
print("\t\tThank You for using BMI calculator by Joe Saju!")
print("\n\t\t\t\tPress ENTER to Exit.")
break
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/359204.html
