1 回答
TA贡献1833条经验 获得超4个赞
你的问题是不正确的缩进:
for i in range(0,len(password)):
for j in range(0,len(guess)):
if(guess[j]==password[i]):
numCorrect=numCorrect+1
for i in range(0,len(password)):
if(guess[i]==password[i]):
posNumCorrect=posNumCorrect+1
您的外循环的索引为i,然后您可以在第二个内循环中劫持并更改该索引。这将i超过循环限制,在第一次迭代时退出外循环。
您需要将第二个“内部”循环和所有后续代码拉回到其正确位置,向左缩进一个,离开第一个循环。
def reportResult(password,guess):
'''Reports users results, see if positions/numbers are correct'''
numCorrect = 0
posNumCorrect = 0
for i in range(len(password)):
for j in range(len(guess)):
if(guess[j] == password[i]):
numCorrect = numCorrect+1
for i in range(len(password)):
if(guess[i] == password[i]):
posNumCorrect = posNumCorrect+1
if(posNumCorrect == 5):
return True
else:
print(numCorrect," of 5 correct digits.")
print(posNumCorrect," of 5 correct locations.")
return False
输出:
I've set my password, enter 5 digits in the range [1-9] (e.g. 9 3 2 4 7):
[8, 2, 4, 5, 6]
10 guesses remaining.
> 8 3 4 5 6
4 of 5 correct digits.
4 of 5 correct locations.
9 guesses remaining.
> 3 8 4 6 5
4 of 5 correct digits.
1 of 5 correct locations.
8 guesses remaining.
>
添加回答
举报