Python Forum
Getting my choices to display fro some reason it isnt working. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Getting my choices to display fro some reason it isnt working. (/thread-38514.html)



Getting my choices to display fro some reason it isnt working. - dgizzly - Oct-24-2022

choice = 0

while choice != QUIT:
    #Display menu & prompt user for choice
    print("\nChoose one of the following options")
    print("\t1. Display 10 ways to cut 500 calories")
    print("\t2. Generate next semester expected weight table")
    print("\t3. Quit")
    choice = int(input("option: "))


#Calories cutting options
if choice==1:
    print("\nTry these 10 ways to cut 500 calories every day...")
    print("*Swap your snack")
    print()



RE: Getting my choices to display fro some reason it isnt working. - deanhystad - Oct-24-2022

Look at your code. You have a loop:
while choice != QUIT:
When does the loop exit? After the loop exits, what is the value of choice?

Another thing to think about. Why are you doing this?
choice = int(input("option: "))
You have the user input "1", "2", "3" etc. Do you actually need to convert those number strings to an int? Are you planning to do math? int(str) is fraught with peril and should be used only when you really need a number for doing number things. If I run your program and type "a <enter>" the program crashes. If I type "1.0 <enter>" the program crashes. If I type anything that is not a valid int string, the program crashes.

I would write the choice input something like this:
DISPLAY = "1"
GENERATE = '2'
QUIT = '3'
 
while True:
    #Display menu & prompt user for choice
    print("\nChoose one of the following options")
    print("\t1. Display 10 ways to cut 500 calories")
    print("\t2. Generate next semester expected weight table")
    print("\t3. Quit")
    choice = input("option: ")
    if choice in (DISPLAY, GENERATE, QUIT):
        break
    print(f"{choice} is not a valid choice\n\n")
This part only gets the choice and validates. You'll have to implement the different actions, including quit.


RE: Getting my choices to display fro some reason it isnt working. - rob101 - Oct-24-2022

For the menu set-up, I would do something like this:

options = (
    "Display 10 ways to cut 500 calories",
    "Generate next semester expected weight table",
    "Quit"
)

choice = 0
while choice == 0:
    for option in enumerate(options, 1):
        print(option[0], option[1])
    choice = input("\nOption: ")
    if choice.isnumeric():
        choice = int(choice)
        if choice > len(options):
            choice = 0
        else:
            pass  # place holder
    else:
        choice = 0
That way, you can add to or alter the 'options' by simply editing the options tuple.

option in the for loop is a dynamic tuple, which is unpacked in the print() function.

Obviously, the option menu and user feedback can be presented however you choose; the point here is that it would make maintenance quick and easy.


RE: Getting my choices to display fro some reason it isnt working. - wavic - Oct-24-2022

Why do you both give complete answers?

It is not supposed to do it instead of him/her but to help him/her find out why isn't working.


RE: Getting my choices to display fro some reason it isnt working. - rob101 - Oct-24-2022

(Oct-24-2022, 07:38 AM)wavic Wrote: Why do you both give complete answers?

It is not supposed to do it instead of him/her but to help him/her find out why isn't working.

It's not complete.


RE: Getting my choices to display fro some reason it isnt working. - DeaD_EyE - Oct-24-2022

There is no question of the OP.