2015-11-13 51 views
-3

所以我需要计算BMI计算的次数以及在此循环结束时打印的次数。有任何想法吗?如何添加计数器功能?

print("Hello and welcome to the BMI calculator!!") 

user = input("Would you like to go again, Y/N: ") 
while user == "y": 
     height = int(input("Please put in your height in Meters: ")) 
     weight = int(input("Please put in your weight in Kilogram: ")) 
     BMI = weight/ (height*height) 
     if BMI < 18: 
     print("Your BMI is:", BMI, "Eat some more Big Macs, you are    too skinny!") 
     elif BMI > 25: 
     print("Your BMI is:", BMI, "Stop eating all those Big Macs, you are far too fat!") 
     elif BMI >18 < 25: 
     print("Your BMI is:", BMI, "You are a normal and healthy weight, congratulations!!!") 
     user = input("Would you like to go again, Y/N: ") 


input("\nPress the enter key to exit") 
+0

'some_variable + = 1'? – furas

回答

0

相当简单。只需在循环外创建一个变量,并在每次循环开始时增加它。

print("Hello and welcome to the BMI calculator!!") 
count = 0; 
user = input("Would you like to go again, Y/N: ") 
while user == "y": 
    count += 1 #Increase count by one 
    height = int(input("Please put in your height in Meters: ")) 
    weight = int(input("Please put in your weight in Kilogram: ")) 
    BMI = weight/ (height*height) 
    if BMI < 18: 
     print("Your BMI is:", BMI, "Eat some more Big Macs, you are    too skinny!") 
    elif BMI > 25: 
     print("Your BMI is:", BMI, "Stop eating all those Big Macs, you are far too fat!") 
    elif BMI >18 < 25: 
     print("Your BMI is:", BMI, "You are a normal and healthy weight, congratulations!!!") 
    user = input("Would you like to go again, Y/N: ") 

print("You checked your BMI", count, "times.") 
input("\nPress the enter key to exit") 
+0

谢谢,这真的很简单:) –