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.

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

try:
except ValueError:
print("Invalid Entry, you MUST enter a number, please select 0 to 2")

time.sleep(1)

while option != 0:
if option == 1:
print("Processing...")
time.sleep(1)
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))

print()

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("[5] Exit the programme")

while True:
package()
try:
except ValueError:
print("Invalid Entry, you MUST enter a number, please select 1 to 5")

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......")
break
elif option == 5:
print("Thank you for using my menu programme.")
quit()

else:
print("Invalid option, please select 0 to 5")

else:
print("Invalid option, please select 0 to 2")

print()

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."
}
print("***Welcome to City Gym main menu***")
print()
print()
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:

# 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):
print()
print()
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:
"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:
"1": "Our BASIC membership package.",
"2": "Our REGULAR membership package.",
})
``````

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