0

I am currently getting user input with this line of code:

qty = int(input("How many of this item should we start with?"))

As one would expect, it throws an error if a value is entered that cannot be converted to INT.

On Error, I would like to prompt "Please enter a whole number" and return to the previous line requesting input. What is the most "Pythonic" way of achieving this?

NickSentowski
  • 820
  • 13
  • 27
  • 3
    _"What is the most "Pythonic" way of achieving this?"_ In short: Using a `while` loop with `try/except`. See the link above for more info. – Christian Dean Oct 12 '17 at 16:46
  • What have you tried already? Have you googled "user input error handling in python" or something along those lines? – toonarmycaptain Oct 12 '17 at 16:49

1 Answers1

2

Probably, the most pythonic way is to do the following

while True:
    try:
        qty = int(input("How many of this item should we start with?"))
        break
    except ValueError:
        pass     
NS0
  • 6,016
  • 1
  • 15
  • 14