获取一个Try语句以循环直到获得正确的值 [英] Get a Try statement to loop around until correct value obtained
本文介绍了获取一个Try语句以循环直到获得正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让用户输入1到4之间的数字.我有代码来检查数字是否正确,但是我希望代码循环几次直到数字正确为止.有谁知道如何做到这一点?代码如下:
I am trying to get a user to enter a number between 1 and 4. I have code to check if the number is correct but I want the code to loop around several times until the numbers is correct. Does anyone know how to do this? The code is below:
def Release():
try:
print 'Please select one of the following?\nCompletion = 0\nRelease ID = 1\nVersion ID = 2\nBuild ID = 3\n'
a = int(input("Please select the type of release required: "))
if a == 0:
files(a)
elif a == 1:
files(a)
elif a == 2:
files(a)
elif a == 3:
files(a)
else:
raise 'incorrect'
except 'incorrect':
print 'Try Again'
except:
print 'Error'
Release()
我也收到关于输入的异常的错误:
I am also getting an error about the exception I have entered:
kill.py:20: DeprecationWarning: catching of string exceptions is deprecated
except 'incorrect':
Error
感谢您的帮助
推荐答案
def files(a):
pass
while True:
try:
i = int(input('Select: '))
if i in range(4):
files(i)
break
except:
pass
print '\nIncorrect input, try again'
这篇关于获取一个Try语句以循环直到获得正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文