问题描述
我想将输入限制为数字范围(例如n)。
如果我使用while x not in range(n): x = input()
,则无法正常工作。
所以我放了while x not in range(n): x = int(input())
。
除非我不给任何字母,否则它工作正常。
经过一番研究,我想到了以下代码:
x = None
while True:
try:
while x not in range(n+1):
x = int(input("X (1 to "+ str(n)+ ") :"))
if x not in range(n+1):
print("please enter a valid input")
break
except:
print("please enter a valid input")
我想知道是否有任何方法可以缩短代码,例如合并while循环and
或or
。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)