Reputation: 251
How could I force an user to put in an integer? I am wondering if I should a 'while' statement. This is the idea I got from here.
x = raw_input("put in a number")
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
Upvotes: 0
Views: 63
Reputation:
Something like this should work:
while True:
try:
x = int(raw_input("put in a number"))
break
except ValueError:
continue # or maybe print a message here...
Upvotes: 4