Python Forum
Getting my choices to display fro some reason it isnt working.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting my choices to display fro some reason it isnt working.
#1
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()
Reply
#2
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.
Reply
#3
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.
dgizzly likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
There is no question of the OP.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code isnt working abdullahali 5 3,416 Oct-01-2018, 02:31 AM
Last Post: Skaperen
  Homework Teacher isnt helping need some help randyearthchild 2 2,912 Nov-05-2017, 11:32 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020