当我输入一个非整数时,我进入了一个无限循环。我如何解决它?这是我的代码:
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)
答案 0 :(得分:1)
你想要达到这样的目标吗?
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)