如何添加计数器函数?

2024-09-30 16:28:06 发布

您现在位置: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")

Tags: andthetoinyouinputyouris
1条回答
网友
1楼 · 发布于 2024-09-30 16:28:06

相当简单。只需在循环外生成一个变量,并在每次循环开始时递增它。你知道吗

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")

相关问题 更多 >