退出无限循环? [英] exit an infinite loop?
本文介绍了退出无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
输入非整数时,我陷入无限循环.我如何解决它?这是我的代码:
I'm getting into an infinite loop when I enter a non-integer. How do I fix it? Here's my code:
def inputInt(integer):
while True:
try:
result = int(integer)
except ValueError:
print ('Invalid input - Try again')
continue
break
return result
value = input (' Enter a value:')
number = inputInt(value)
print ('The value is',number)
推荐答案
您是否要实现这样的目标?
Are you trying to achieve something like this?
def inputInt(acceptAny):
while True:
try :
value = input (' Enter a value:')
value = int(value)
except ValueError:
if acceptAny:
break
else:
print ('Invalid input - Try again')
continue
break
return value
if __name__ == "__main__":
number = inputInt(False)
print ('The value is',number)
这篇关于退出无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文