Validation 如何验证输入,使其必须是整数

Validation 如何验证输入,使其必须是整数,validation,python-3.x,integer,Validation,Python 3.x,Integer,我正在尝试验证输入,以便它必须是一个整数。我已经验证了输入,因此它必须在某个范围内,但是如果他们输入字母“b”,例如,就会出现一个错误,显示为“ValueError:invalid literl for int(),以10为基数”。如果值不是整数,我希望在else部分显示相同的消息。我已经在网上搜索了一段时间,无法确定如何验证输入,因此它必须是一个整数 Minutes=int(input("How long do you want the interval between each stretc

我正在尝试验证输入,以便它必须是一个整数。我已经验证了输入,因此它必须在某个范围内,但是如果他们输入字母“b”,例如,就会出现一个错误,显示为“ValueError:invalid literl for int(),以10为基数”。如果值不是整数,我希望在else部分显示相同的消息。我已经在网上搜索了一段时间,无法确定如何验证输入,因此它必须是一个整数

Minutes=int(input("How long do you want the interval between each stretch to be?\nIndicate a number in minutes between 5 and 60\n\nAfter the minutes specified, a window with suggested stretches will appear\n--> "))
y=1 当y==1时:

if Minutes in range (4,61):
    TimeMinute (Minutes) #(Minutes needs to be multiplied by 60 to make it into minutes) Minutes is currently in the unit of seconds
    y=0
else:
    Minutes=int(input ("Error. You need to enter a number between 5 and 60.\nHow long do you want the interval between each stretch to be?\nIndicate a number in minutes between 5 and 60\nAfter the minutes specified, a window with suggested stretches will appear\n-->  "))

您应该首先获取用户输入,然后尝试将其解析为整数,如果失败(引发
ValueError
),则打印一条错误消息,如果它是有效整数,则检查范围并执行所需操作

value = input('Enter minutes :')
try:
    value = int(value)
    if 4 < value < 61:
       # Do your stuff
    else:
       print('Error value must be in range: 5-60')
except ValueError:
   print('The value you entered is not a valid integer value')
value=input('输入分钟:')
尝试:
value=int(值)
如果4<值<61:
#做你的事
其他:
打印('错误值必须在5-60'范围内)
除值错误外:
打印('输入的值不是有效的整数值')

我想知道值之后第三行代码中的“10”是什么意思。这将处理文本输入(字母而不是数字)。我想防止输入除整数以外的任何东西,这能解决这个问题吗?10的意思是:假设整数是以10为基数的,转换它们。您还可以转换十六进制、八进制、二进制或任何其他基本字符串。。。这实际上是不必要的,因为默认基数是10。我会找到答案的。这将防止输入除4到61之间的整数以外的任何内容。