关于验证:我有没有办法验证一个数字是否输入了输入?

I there any way to validate a number had been entered into an input?

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

是否有任何方法可以验证用户已使用while循环和变量输入了一个数字,例如:

1
2
3
4
5
6
7
8
9
10
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")


可以使用try/except来区分字符串是否可转换为整数。

1
2
3
4
5
6
7
gotNumber = False
while not gotNumber:
    try:
        num = int(input('>> '))
        gotNumber = True
    except ValueError:
        print("That is not a number.")