-3

I'm taking a fundamentals of programming class and we're supposed to be building a menu that calculates BMI and also shows different gym membership options, what I can't figure out is why my menu keeps looping back to the BMI calculator after viewing the membership rates.

this is some of my code:

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

Any suggestions would be helpful, I've been playing around for a while and can't find the solution.

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34

1 Answers1

1

It looks like the reason for this is that you're using option variable to store the value that user provide for both main menu and sub menu.

Instead of this

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

Use

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

Also replace the option with submenu_option only where you wants to refer to the user selection for the sub menu