【问题标题】:Loop keeps returning to wrong part循环不断返回错误的部分
【发布时间】: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:  "))
  

任何建议都会有所帮助,我已经玩了一段时间但找不到解决方案。

【问题讨论】:

标签: python menu bmi


【解决方案1】:

看起来这是因为您正在使用 option 变量来存储用户为主菜单和子菜单提供的值。

而不是这个

submenu()
option = int(input("Enter your option:  "))

利用

submenu()
submenu_option = int(input("Enter your option:  "))

仅在您想要引用子菜单的用户选择的地方将 option 替换为 submenu_option

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 2018-01-24
    • 2018-11-09
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多