我的代码没有正确计算正确的数字,
示例:(注意,我是故意打印密码的(列表))
I've set my password, enter 5 digits in the range [1-9] (e.g. 9 3 2 4 7):
[1, 5, 6, 7, 8]
10 guesses remaining.
>
1 5 6 7 9
1 of 5 correct digits.
4 of 5 correct locations.
9 guesses remaining.
>
5 位数字中有 4 位是正确的,只计算“5 中的 1”。
这是我的代码:
import random
def generatePassword():
'''Generates unique 5 digit password'''
randomSetOfValues = set()
while len(randomSetOfValues) < 5:
randomSetOfValues.add(random.randint(1,9))
return list(randomSetOfValues)
def getUserGuess():
'''Gets users geuss/input for the password'''
guess_list=[]
print('>',)
a,b,c,d,e=map(int, input().split())
guess_list.append(a)
guess_list.append(b)
guess_list.append(c)
guess_list.append(d)
guess_list.append(e)
return guess_list
def reportResult(password,guess):
'''Reports users results, see if positions/numbers are correct'''
numCorrect=0
posNumCorrect=0
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
if(posNumCorrect==5):
return True
else:
print(numCorrect," of 5 correct digits.")
print(posNumCorrect," of 5 correct locations.")
return False
我认为问题出在我的 reportResult 函数中;我被困住了。
潇潇雨雨
相关分类