我的代码未正确计算正确的数字,
示例:(注意,我是故意(在清单上)打印了密码)
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分之一”。
这是我的代码:
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
def main():
'''main function'''
#Continue loop
con="y"
while(con=="y"):
#set count=10
guess_count=10
#call pswd generator
origin_pswd=generatePassword()
print("I've set my password, enter 5 digits in the range [1-9] (e.g. 9 3 2 4 7):\n")
print(origin_pswd) #used to test
#loop unti 10 guess or correct guess
while(guess_count!=0):
print(guess_count," guesses remaining.")
#get user input
guess_list=getUserGuess()
#Call check
if(reportResult(origin_pswd,guess_list)):
print("You got my treasure! Congratulations. You correctly geussed the password:",*origin_pswd)
print()
break;
else:
guess_count=guess_count-1
if(guess_count==0):
print("You'll never get my treasure! The password was ",*origin_pswd)
print()
break;
#Loop continuation prompt
print("Would you like to play again? (y/n) ")
con=input("> ")
while(con!="y" and con!="n"):
print("Error!!!Please eneter y/n")
ch=input("> ")
if (con =="n"):
print("Thanks for playing!")
#call main
if __name__== "__main__":
main()
我认为问题将出在我的reportResult函数中;我被卡住了。
答案 0 :(得分:1)
您的问题是缩进不正确:
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
超出循环限制,并在第一次迭代时退出外部循环。
您需要拉出第二个“内部”循环,然后将所有后续代码放回其正确位置,即在第一个循环的左侧 out 处向左缩进。
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.
>