Python 如何添加计数器功能?
所以我需要计算BMI的计算次数,并在循环结束时打印出来。有什么想法吗Python 如何添加计数器功能?,python,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
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(“您好,欢迎使用体重指数计算器!!”)
用户=输入(“您想再去吗,Y/N:”)
当用户==“y”时:
高度=int(输入(“请以米为单位输入高度:”)
重量=整数(输入(“请输入您的重量,单位为千克:”)
体重指数=体重/(身高*身高)
如果体重指数<18:
打印(“你的BMI是:”,BMI,“多吃些巨无霸,你太瘦了!”)
如果体重指数>25:
打印(“你的BMI是:”,BMI,“不要吃那些巨无霸,你太胖了!”)
如果体重指数>18<25:
打印(“你的BMI是:”,BMI,“你的体重正常健康,恭喜!!!”)
用户=输入(“您想再去吗,Y/N:”)
输入(“\n按enter键退出”)
相当简单。只需在循环外创建一个变量,并在每次循环开始时递增它
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")
print(“您好,欢迎使用体重指数计算器!!”)
计数=0;
用户=输入(“您想再去吗,Y/N:”)
当用户==“y”时:
计数+=1#将计数增加1
高度=int(输入(“请以米为单位输入高度:”)
重量=整数(输入(“请输入您的重量,单位为千克:”)
体重指数=体重/(身高*身高)
如果体重指数<18:
打印(“你的BMI是:”,BMI,“多吃些巨无霸,你太瘦了!”)
如果体重指数>25:
打印(“你的BMI是:”,BMI,“不要吃那些巨无霸,你太胖了!”)
如果体重指数>18<25:
打印(“你的BMI是:”,BMI,“你的体重正常健康,恭喜!!!”)
用户=输入(“您想再去吗,Y/N:”)
打印(“你检查了你的体重指数”,计数,“次数”)
输入(“\n按enter键退出”)
一些变量+=1
?谢谢你,这确实很简单:)