如何添加计数器功能?

时间:2015-11-13 17:48:02

标签: python

所以我需要计算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")

1 个答案:

答案 0 :(得分: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")