簡體   English   中英

如何添加計數器功能?

[英]How to add a counter function?

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

相當簡單。 只需在循環外創建一個變量,並在每次循環開始時增加它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM