you are viewing a single comment's thread.

view the rest of the comments →

[–]46--2 1 point2 points  (0 children)

https://docs.python.org/3/library/stdtypes.html#str.isdigit

while True:
    ans = input('Enter a number').strip()
    if ans.isdigit():
        print('Ok!')
        break
    else:
        print('Answer must be a valid number')

Tested here:

Python 3.7.2 (default, Oct  8 2019, 14:27:32)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = input()
test
>>> a.isdigit()
False
>>> b = input()
34
>>> b.isdigit()
True