Reputation: 700
What I'm trying to do is ask users for two inputs, if one of the inputs is less than zero or if the input is some string then ask for the inputs again. The only valid input are numbers >=0. So you can't have something like -1 or cat as inputs.
Attempt 1:
number_of_books = input("What is the number of books in the game?: ")
number_of_chairs = input("What is the number of chairs in the game?: ")
while int(number_of_books) < 0 or int(number_of_chairs) < 0 or isinstance(number_of_books, str) or isinstance(number_of_chairs, str):
print("Input cannot be less than 0 or cannot be some string.")
number_of_books = input("What is the number of books in the game?: ")
number_of_chairs = input("What is the number of chairs in the game?: ")
But from this I realized that the input are always strings so it will ask for input again.
Attempt 2:
number_of_books = int(input("What is the number of books in the game?: "))
number_of_chairs = int(input("What is the number of chairs in the game?: "))
while number_of_books < 0 or number_of_chairs < 0 or isinstance(number_of_books, str) or isinstance(number_of_chairs, str):
print("Input cannot be less than 0 or cannot be some string.")
number_of_books = int(input("What is the number of books in the game?: "))
number_of_chairs = int(input("What is the number of chairs in the game?: "))
But with this one I realized that you cannot do int('some string')
So I'm wondering if there is a way to do this or is this something not possible?
Upvotes: 0
Views: 657
Reputation: 4425
Use the try except method
while stringval = input("What is the number you want to enter"):
try:
intval = int(stringval)
if intval > -1:
break
except ValueError:
print 'Invalid answer, try again'
# Process intval now
Upvotes: 0
Reputation: 23231
You should use a while
loop for each input, so only the invalid one is asked again. By using while True
, you ask for input once, and break once it's valid. Try to convert to int
, which raises ValueError
if that cannot be done. Exceptions cause the custom error to be printed, and the loop restarted.
while True:
books_input = input("What is the number of books in the game?: ")
try:
number_of_books = int(books_input)
if number_of_books < 0:
raise ValueError
except ValueError:
print('Input must be an integer and cannot be less than zero')
else:
break
You'd need to do this for each input (maybe there are more than two?), so it makes sense to create a function to do the heavy lifting.
def non_negative_input(message):
while True:
input_string = input(message)
try:
input_int = int(books_input)
if input_int < 0:
raise ValueError
except ValueError:
print('Input must be an integer and cannot be less than zero')
else:
break
return input_int
Calling it:
non_negative_input("What is the number of books in the game?: ")
Upvotes: 4
Reputation: 11728
The solution is to coerce the answer to int() and catch any exceptions, for example:
while True:
try:
number_of_books = int(raw_input('Enter number of books:'))
if number_of_books < 0:
raise ValueError('must be greater than zero')
except ValueError, exc:
print("ERROR: %s" % str(exc))
continue
break
Probably you should put this in a function, and make the prompt a variable (to allow re-use).
Upvotes: 0