1

I have seen many answers to this question but am looking for something very specific. What I need to accomplish (in pseudo code) is this:

> FOR every ITEM in DICTIONARY, DO:
>           PROMPT user for input
>           IF input is integer
>                 SET unique-variable to user input

I'm very new to Python so the code may not be proper, but here is what I have:

def enter_quantity():
  for q in menu:
      quantities[q] = int(input("How many orders of " + str(q) + "?: "))

So this does everything but evaluate the user input. The problem I'm having is if the input is incorrect, I need to re-prompt them for the same item in the top-level for loop. So if it's asking "How many slices of pizza?" and the user inputs "ten", I want it to say "Sorry that's not a number" and return to the prompt again of "How many slices of pizza?".

Any/all ideas are appreciated. Thanks!


My final solution:

def enter_quantity():
for q in menu:
    booltest = False
    while booltest == False:
        inp = input("How many orders of " + str(q) + "?: ")
        try:
            int(inp)
            booltest = True
        except ValueError:
            print (inp + " is not a number. Please enter a nermic quantity.")
    quantities[q] = int(inp)

2 Answers 2

2

You need a while loop with a try/except to verify the input:

def enter_quantity():
    for q in menu:
        while True:
            inp = input("How many orders of {} ?: ".format(q))
            try:
               inp = int(inp) # try cast to int
               break
            except ValueError:
                # if we get here user entered invalid input so print message and ask again
                print("{} is not a number".format(inp))
                continue
        # out of while so inp is good, update dict
        quantities[q] = inp
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry! I omitted a piece of code... first post, I'll edit it to reflect that there was a FOR loop there. Thanks for your comment, I will try it out.
No prob, the main thing to take from the example is how to use a while loop with a try/except to verify the input. I updated my example
Awesome. I figured out my solution and am going to mark your answer and show my code. Really appreciate the second set of eyes! Learning Python has been a lot of fun so far.
No prob, just so you know my code does what yours does we only leave the while each iteration when we get valid input
0

This bit of code is a little more useful if a menu is added otherwise it crashes at the first hurdle. I also added a dictionary to store the input values.

menu = 'pizza', 'pasta', 'vino'
quantities = {}
def enter_quantity():

    for q in menu:
        while True:
            if q == 'pizza':
                inp = input(f"How many slices of {q} ?: ")
            elif q == 'pasta':
                inp = input(f"How many plates of {q} ?: ")
            elif q == 'vino':
                inp = input(f"How many glasses of {q} ?: ")
            try:
               inp = int(inp) # try cast to int
               break
            except ValueError:
                # exception is triggered if invalid input is entered. Print message and ask again
                print("{} is not a number".format(inp))
                continue
        # while loop is OK, update the dictionary
        quantities[q] = inp

    print(quantities) 

Then run the code from this command:

enter_quantity()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.