我使用的是python2.6.6
我有这个代码:height = 20
width = 10
x = input('Please insert a number between ' + str(width + 1) + ' and ' + str(height) + ': ')
while x < (width + 1) or x > 20:
print 'That option is not valid'
x = input('Please insert a number between ' + str(width + 1) + ' and ' + str(height) + ': ')
如果用户只输入数字,一切正常,但如果用户出错并键入,例如q,则给出:NameError: name 'q' is not defined
我希望,如果用户插入一个字符串,while循环就会启动并给用户:该选项无效。。。。
既然宽度和高度我想把tham当作数字来处理,那么如何在不使用原始输入的情况下解决这个问题呢?在
谨致问候
法拉斯
编辑
根据丹尼尔的建议,我将代码修改为:
^{pr2}$
如果用户只输入int代码,则代码按计划工作,但它不安全,不会出现用户错误。如果用户出错并键入“q”,则会出现以下错误:ValueError: invalid literal for int() with base 10: 'q'
我明白为什么,但我该怎么解决呢?在
谨致问候
法拉斯