我有以下代码段:
def check1(n):
if len(n) != 4:
return raw_input("Enter 4 digits only")
else:
return True
def check2(n):
if n.isdigit() != True:
return raw_input("Enter digits only")
else:
return True
def check3(n):
if len(set(str(n))) != 4:
return raw_input("Enter non duplicate numbers only")
else:
return True
sturn = 1
lturn = 8
a = raw_input("Enter the 4 numbers you want to play with: ")
for turn in range(sturn, lturn):
b = raw_input("Enter your guess: ")
if (check1(b) != True or check2(b) != True or check3(b) != True):
if check1(b) != True:
print check1(b)
elif check2(b) != True:
print check2(b)
elif check3(b) != True:
print check3(b)
else:
print b
如何重写这一点,如果任何检查功能失败,它将再次从b = raw_input
行开始,并重新检查所有检查。
更新 在听取了keithjgrant和m1k3y02的建议后,我改进了代码,但它无法正常工作。如果我连续输入'1',它会在不同的异常之间反弹,而不是留在第一次检查。
def checks(n):
if len(n) != 4 or n.isdigit() != True or len(set(str(n))) != 4:
return False
else:
return True
sturn = 1
lturn = 8
a = raw_input("Enter the 4 numbers you want to play with: ")
for turn in range(sturn, lturn):
b = raw_input("Enter your guess: ")
while checks(b) != True:
if len(b) != 4:
b = raw_input("Enter 4 digits only")
if b.isdigit() != True:
b = raw_input("Enter digits only")
if len(set(str(b))) != 4:
b = raw_input("Enter non duplicate numbers only")
print b
答案 0 :(得分:3)
您的问题,就目前而言,与例外无关。请尝试以下方法:
checks = [
lambda n: (len(n)==4, "Enter 4 digits only."),
lambda n: (n.isdigit(), "Enter digits only."),
lambda n: (len(set(str(n)))==4, "Enter non duplicate numbers only.")
]
a = raw_input("Enter the 4 numbers you want to play with: ")
sturn = 1
lturn = 8
for turn in range(sturn, lturn):
b = raw_input("Enter your guess: ")
all_good = True
for check in checks:
good,msg = check(b)
if not good:
print msg
all_good = False
break
if all_good:
print "{0} is correct!".format(b)
break
您可以将其重写为使用例外,如下所示:
class CheckError(Exception): pass
class WrongLengthError(CheckError): pass
def check1(n):
if len(n) != 4:
raise WrongLengthError("Enter 4 digits only.")
class NonDigitCharError(CheckError): pass
def check2(n):
if not n.isdigit():
raise NonDigitCharError("Enter digits only.")
class HasDuplicatesError(CheckError): pass
def check3(n):
if len(set(str(n))) != 4:
raise HasDuplicatesError("Enter non duplicate numbers only.")
a = raw_input("Enter the 4 numbers you want to play with: ")
sturn = 1
lturn = 8
for turn in range(sturn, lturn):
b = raw_input("Enter your guess: ")
try:
for check in (check1, check2, check3):
check(b)
print "{0} is correct!".format(b)
break
except CheckError, c:
print c
答案 1 :(得分:1)
你应该尝试创建包含所有检查的包装函数。一旦失败,一次又一次地调用该函数
答案 2 :(得分:1)
你是什么意思“从第一个开始?”你的意思是回到raw_input?你可以把它放一段时间并循环,直到你有一个有效的输入。
我还建议将检查功能合并为一个。当你真的不需要时,你也会给每个人打两次电话;只需捕捉响应即可。
valid_response = False
while not valid_response:
input = raw_input("Enter your guess: ")
valid_response = checkInput(input)
if not valid_response:
print valid_response
else:
print input
即使这样可以进一步清理,但希望它会指向正确的方向。使用valid_response
来处理字符串或布尔值时,我觉得很蠢;你可以找到一种方法来重新思考你的检查功能是如何工作的。
<强>更新强>
您遇到了一些问题:您仍在多次执行支票,并且您正在将验证逻辑与raw_input()
来电相结合。因为您要检查输入的有效性(布尔值)和无效数据的原因(更多选项),所以不使用外部函数可能更有意义。特别是因为我们只讨论了几行代码。试试这个:
sturn = 1
lturn = 8
# you never do anything with this variable "a". Did you mean to type "b" here?
a = raw_input("Enter the 4 numbers you want to play with: ")
for turn in xrange(sturn, lturn):
valid_input = False
b = raw_input("Enter your guess: ")
while not valid_input:
if len(b) != 4:
b = raw_input("Enter 4 digits only")
elif b.isdigit() != True:
b = raw_input("Enter digits only")
elif len(set(str(b))) != 4:
b = raw_input("Enter non duplicate numbers only")
else:
valid_input = True # breaks you out of your while loop
print b
我在这段代码中没有真正评估的一件事是这三项检查是否有效,因为我不太清楚你要用它们做什么。如果您还没有,可能值得单独测试每一个,以确保它们实际上以您需要的方式验证用户输入。