Python 如果用户输入字符串而不是整数,则出现异常
如果用户键入的是字符串而不是整数,我将尝试打印程序(输入无效,请重试)Python 如果用户输入字符串而不是整数,则出现异常,python,Python,如果用户键入的是字符串而不是整数,我将尝试打印程序(输入无效,请重试) num_dice = 0 while True: if num_dice < 3 or num_dice > 5 or num_dice: num_dice = int(input("Enter the number of dice you would like to play with 3-5 : ")) print("") print("Please enter
num_dice = 0
while True:
if num_dice < 3 or num_dice > 5 or num_dice:
num_dice = int(input("Enter the number of dice you would like to play with 3-5 : "))
print("")
print("Please enter a value between 3 and 5.")
print("")
continue
else:
break
num\u dice=0
尽管如此:
如果num_dice<3或num_dice>5或num_dice:
num_dice=int(输入(“输入您想玩的骰子数量3-5:”)
打印(“”)
打印(“请输入一个介于3和5之间的值”)
打印(“”)
持续
其他:
打破
您只需使用isnumeric
关键字检查它是否为绝对数
示例:
string1="123"
string2="hd124*"
string3="helloworld"
if string1.isnumeric() is True:
#Proceed with your case.
inputs=string1
文档参考:
另外,这需要您将输入更改为字符串格式,因为isnumeric
只验证字符串
我的意思是下面这部分
num\u dice=str(输入(“输入您想玩的3-5个骰子的数量:”)
不客气:)