Robert Birch
Robert Birch

Reputation: 251

Force a user to put in a intger

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

Answers (1)

user2555451
user2555451

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

Related Questions