我如何让Python检查变量以查看它是否是一个整数且低于特定数字然后继续,具体取决于结果可能会从循环中断开。 所以大概是这样的:
x = input
if x is integer AND below 10:
print ("X is a number below 10")
break from loop
elif x is NOT an integer:
print ("X is not an integer try again")
ask again
Else:
print ("Error")
ask again
答案 0 :(得分:1)
非常确定这是有效的:D谢谢
而True:
x = input("Enter Something: ") #user input
if x.isdigit() == True:
if (int(x)>=1) and (int(x)<=2): #if it is 1 or 2
print (x)
break
elif (int(x)>2): #or if its above 2
print ("To Large")
else:
print ("Else")
elif x.isdigit() == False: #data non integer
print ("please try again")
else:
print ("ERROR") #error with input
打印(&#34; Hello&#34;)#显示循环正确破坏