How to check if string input is a number?

Simply try converting it to an int and then bailing out if it doesn’t work.

try:
    val = int(userInput)
except ValueError:
    print("That's not an int!")

See Handling Exceptions in the official tutorial.

Leave a Comment