【发布时间】:2022-11-28 14:50:42
【问题描述】:
我正在上编程课的基础知识,我们应该构建一个菜单来计算 BMI 并显示不同的健身房会员选项,但我不明白的是为什么我的菜单在查看后一直循环回到 BMI 计算器会员费率。
这是我的一些代码:
def mainmenu():
option = int(input("Enter your option: "))
while option != 0:
if option == 1:
try:
print("Calculate BMI")
the_height = float(input("Enter the height in cm: "))
assert the_height > 0
the_weight = float(input("Enter the weight in kg: "))
assert the_weight > 0
the_BMI = the_weight / (the_height/100)**2
except ValueError:
print("Enter height and weight in whole numbers")
print("Your BMI is", the_BMI)
if the_BMI <= 18.5:
print("You are underweight.")
elif the_BMI <= 24.9:
print("You are Normal.")
elif the_BMI <= 29.9:
print("You are overweight.")
else:
print("You are obese.")
check = input("Do you want to quit or start again, enter Y to restart or another to end ?: ")
if check.upper() == "Y":
print("Bye...")
mainmenu()
elif option == 2:
def submenu():
print("Choose your membership type")
print("[1] Bassic")
print("[2] Regular")
print("[3] Premium")
print("[0] Exit to main menu")
loop = True
while loop:
submenu()
option = int(input("Enter your option: "))
if option == 1:
print("Basic Membership")
print("$10 per week, $40 per month")
break
elif option == 2:
print("Regular Membership")
print("$15 per week, $60 per month")
check = input("Do you want to quit or start again, enter Y to restart or another to end ?: ")
if check.upper() == "Y":
submenu()
elif option == 3:
print("Premium Membership")
print("$20 per week, $80 per month")
check = input("Do you want to quit or start again, enter Y to restart or another to end ?: ")
if check.upper() == "Y":
submenu()
elif option == 0:
loop = False
else:
break
else:
print("Invalid option....")
break
mainmenu()
option = int(input("Enter your option: "))
任何建议都会有所帮助,我已经玩了一段时间但找不到解决方案。
【问题讨论】: