How to check user input (Python)

Ryan

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)
Padraic Cunningham

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check user input (Python)

From Dev

Python how can you check for consecutive letters in a user input?

From Dev

How to check if a user input is a float

From Dev

how to check if user input is in a dictionary

From Dev

Python 3: Specific user input check

From Dev

Python: Check folder directory given by user input

From Dev

How to check if user input is a float or int in java?

From Dev

How to check if user input is not an int value

From Dev

how to check if user input is a certain character

From Dev

How to check if user input is a certain base?

From Dev

JavaFX - How to check user input in editable TableView

From Dev

How to validate user input in python

From Dev

How to validate user input in python

From Dev

Check if user input is in array

From Dev

Check if the user input fits

From Dev

python subprocess: check to see if the executed script is asking for user input

From Dev

How do I check if input is a fraction (Python)?

From Dev

How do I check if input is a number in Python?

From Dev

How to check if input is a natural number in Python?

From Dev

How do I check an input for an integer in Python?

From Dev

How do I check if input is a number in Python?

From Dev

How do you check if input is numerical in Python?

From Dev

How to check for user input error when initializing a class in java

From Java

How to check if multiple strings are present in a txt file through user input?

From Dev

How to use an if statement to check if user input is a certain variable type?

From Dev

How to check if a user has stopped typing in an input field?

From Dev

How to check in JS if user selected a file in file input on Android 2.3.7

From Dev

How do I check if a user input exist in Parser.com

From Dev

How can I check if the user input with the database information?

Related Related

  1. 1

    How to check user input (Python)

  2. 2

    Python how can you check for consecutive letters in a user input?

  3. 3

    How to check if a user input is a float

  4. 4

    how to check if user input is in a dictionary

  5. 5

    Python 3: Specific user input check

  6. 6

    Python: Check folder directory given by user input

  7. 7

    How to check if user input is a float or int in java?

  8. 8

    How to check if user input is not an int value

  9. 9

    how to check if user input is a certain character

  10. 10

    How to check if user input is a certain base?

  11. 11

    JavaFX - How to check user input in editable TableView

  12. 12

    How to validate user input in python

  13. 13

    How to validate user input in python

  14. 14

    Check if user input is in array

  15. 15

    Check if the user input fits

  16. 16

    python subprocess: check to see if the executed script is asking for user input

  17. 17

    How do I check if input is a fraction (Python)?

  18. 18

    How do I check if input is a number in Python?

  19. 19

    How to check if input is a natural number in Python?

  20. 20

    How do I check an input for an integer in Python?

  21. 21

    How do I check if input is a number in Python?

  22. 22

    How do you check if input is numerical in Python?

  23. 23

    How to check for user input error when initializing a class in java

  24. 24

    How to check if multiple strings are present in a txt file through user input?

  25. 25

    How to use an if statement to check if user input is a certain variable type?

  26. 26

    How to check if a user has stopped typing in an input field?

  27. 27

    How to check in JS if user selected a file in file input on Android 2.3.7

  28. 28

    How do I check if a user input exist in Parser.com

  29. 29

    How can I check if the user input with the database information?

HotTag

Archive