My menu is asking me to enter things twice?

Hi All
I have nearly completed this assignment, but I am stuck on why the menu is now asking me to enter my responses twice!! grrrr…I am just a beginner, so sorry for the painful questions. This is the error below:: Can someone help me fix this last part!
Welcome to City Gym main menu

[1] Calculate body mass index BMI
[2] Veiw membership cost
[0] Exit the program.
Please enter your option: 1
Please enter your option:

Here is my code:
import time

  def menu():
      print("***Welcome to City Gym main menu***")
      print()
      print("[1] Calculate body mass index BMI")
      print("[2] Veiw membership cost")
      print("[0] Exit the program.")
  
  menu()
  try:
      option = int(input("Please enter your option: "))
  except ValueError:
      print("Invalid Entry, you MUST enter a number, please select 0 to 2")
  
  option = int(input("Please enter your option: "))
  time.sleep(1)
  
  while option != 0:
      if option == 1:
          Height = float(input('Please enter your height in meters: '))
          print("Processing...")
          time.sleep(1)
          Weight = float(input('Please enter your weight in kilograms: '))
          print("Processing...")
          time.sleep(1)
  
          def BMI(Height, Weight):
                  BMI = round(Weight/(Height**2),3)
  
                  if (BMI < 18.5):
                          return 'Underweight', BMI
                  elif (BMI >= 18.5 and BMI < 25):
                          return 'Normal', BMI
                  elif (BMI >= 25 and BMI < 30):
                          return 'Overweight', BMI
                  elif (BMI >= 30):
                          return 'Obese', BMI
          quote, BMI = BMI(Height, Weight)
          print('Your BMI is: {} and you are: {}'.format(BMI, quote))
  
          anykey=input("Enter any key to return to main menu: ")
          print()
          menu()
  
      elif option == 2:
          def package():
                  print()
                  print("***Welcome to City Gyms Member page***")
                  print()
                  print("[1] Our BASIC membership package")
                  print("[2] Our REGULAR membership package")
                  print("[3] Our PREMIUM membership package")
                  print("[4] Return to MAIN menu")
                  print("[5] Exit the programme")
  
          while True:
              package()
              try:
                  option = int(input("Please enter your option: "))
              except ValueError:
                  print("Invalid Entry, you MUST enter a number, please select 1 to 5")
                  
              option = int(input("Please enter your option: "))
              print("Processing...")
              time.sleep(1)
              if option == 1:
                  print("Our BASIC membership cost is $10 per week")
                  base_weekly_cost = 10
                  base_weekly_cost = base_weekly_cost * 4
  
                  print("The monthly cost will be, $" + str(base_weekly_cost))
              elif option == 2:
                  print("Our REGULAR membership cost is $15 per week")
                  base_weekly_cost = 15
                  base_weekly_cost = base_weekly_cost * 4
  
                  print("The monthly cost will be, $" + str(base_weekly_cost))
              elif option == 3:
                  print("Our PREMIUM membership plan costs $20 per week")
                  base_weekly_cost = 20
                  base_weekly_cost = base_weekly_cost * 4
  
                  print("The monthly cost will be, $" + str(base_weekly_cost))
              elif option == 4:
                  print("Hold on 2 seconds, returning you to the MAIN menu......")
                  menu()
                  break
              elif option == 5:
                  print("Thank you for using my menu programme.")
                  quit()
              
              else:
                  print("Invalid option, please select 0 to 5")
                  
              anykey=input("Enter any key to return to membership menu: ")
                              
      else:
          print("Invalid option, please select 0 to 2")
  
      print()        
      menu()
      option = int(input("Please enter your option: "))
  
  print("Thank you for using my menu programme.")

It’s asks for the option in the try block, and then it asks again after the try block.

Just a little constructive advice, if I may.

There are a few different ways to do this; one way is to design your menus, as functions, then adding (or altering) options becomes easier.

As an example:

def main_menu():
    menu = {"1": "Calculate body mass index BMI.",
            "2": "View membership cost.",
            "3": "Exit the program."
            }
    options = menu.keys()
    print("***Welcome to City Gym main menu***")
    print()
    for item in menu:
        print(f"[{item}] {menu[item]}")
    print()
    option = input("Please enter your option: ")
    if option not in options:
        print(
            f"Invalid Entry, you MUST enter a number, please select 1 to {len(options)}\n")
        return False
    else:
        return int(option)


option = False
while not option:
    option = main_menu()

# this print function is simply so that you can see what was returned
print(f"option {option} returned")

Now, all you need to do, whenever you want a new menu, is to edit the menu items, the heading and rename the function; the rest of the code remains as is – a simple C&P


Just to extend my idea a little:

You could even have one menu function, then pass the items to it, like this:

def menu(menu):
    options = menu.keys()
    print()
    for item in menu:
        print(f"[{item}] {menu[item]}")
    print()
    option = input("Please enter your option: ")
    if option not in options:
        print(
            f"Invalid Entry, you MUST enter a number, please select 1 to {len(options)}\n")
        return False
    else:
        return int(option)

option = False
print("***Welcome to City Gym main menu***")
while not option:
    option = menu(menu={
        "1": "Calculate body mass index BMI.",
        "2": "View membership cost.",
        "3": "Exit the program."
    })

option = False
print("***Welcome to City Gyms Member page***")
while not option:
    option = menu(menu={
        "1": "Our BASIC membership package.",
        "2": "Our REGULAR membership package.",
        "3": "Our PREMIUM membership package.",
        "4": "Return to MAIN menu."
    })

To add:
Another thing that I’ve noticed in your code is that you’re not using f-strings (which is a far more up-to-date way of coding) and you’re using more constants than you need to.

This:

print("Our BASIC membership cost is $10 per week")
base_weekly_cost = 10
base_weekly_cost = base_weekly_cost * 4
print("The monthly cost will be, $" + str(base_weekly_cost))

… can be replaced with this:

weekly_cost = 10
print(f"Our BASIC membership cost is ${weekly_cost:2.2f} per week.")
print(f"The monthly cost will be ${(weekly_cost*52)/12:2.2f}")

… and if you base all of the calculations on the weekly_cost constant, all the other costs will fall in line – one change is all that is needed, if the subscription costs alter.

I hope that this is not too over-loaded; I know there’s quite a lot to take in, but given what you’ve done so far, I’m sure that you will be able to understand what I’ve said and learn something from that.

1 Like

Rob, thank you for this advice…it really really helps

1 Like