-1
print("Now please enter a number")
No1 = int(input(">> "))

Is there any way that I could validate that the user had entered a number using a while loop and variables for example:

NumberInput1 = False

print("Now please enter a number")
while NumberInput = False
    No1 = int(input(">> "))
    #if a number was entered
    NumberInput1 = True
    #if a number wasn't entered
    NumberInput1 = False
    print("That is not a number try again")
  • 1
    Possible duplicate of [How can I type-check variables in Python?](http://stackoverflow.com/questions/463604/how-can-i-type-check-variables-in-python) – rll Dec 10 '15 at 19:31

1 Answers1

1

You can use try/except to distinguish whether your string is convertible to an integer.

gotNumber = False
while not gotNumber:
    try:
        num = int(input('>> '))
        gotNumber = True
    except ValueError:
        print("That is not a number.")
khelwood
  • 55,782
  • 14
  • 81
  • 108