这是我写的代码,它运行得很好,这段代码的函数也是如此,除了一部分,当我问用户是否想再试一次的时候,如果他们选择了否,那么它就停止了,这是应该发生的。另一方面,如果他们说“是”,他们会得到另一个完全相同的提示“你想再试一次吗?”?(是/否)。你可以说y 100次,什么都不会发生,我的目标是让代码返回并从一开始就调用函数。我尝试了一个休息,如果用户说'y'试图摆脱循环等,但它没有工作,我现在不知道
另外,如您所见,我有正确的数字来比较用户猜测的数字是否在生成的列表中,这部分我没有问题。现在有了正确的位置,我不知道该怎么做,目标是检查两个列表中的数字和位置都是名称。你知道吗
import random
play = True
turnsleft = 1
#this is a function that is in charge of generating a random password
def generatePassword():
generatePassword = [] #create an empty list
for i in range(1,6):
generatePassword.append(random.randint(1,9))
return generatePassword
'''this is a function that prompts the userfor their guess
the input will be comprimised of 5 different variables to store
the different numbers which will then all be added to the list of user number'''
def getUserGuess():
getUserGuess = [] #create an empty list
v1,v2,v3,v4,v5 = input("Please take a guess of the password by entering 5 numbers(comma between each): ").split(",")
v1,v2,v3,v4,v5 = int(v1), int(v2), int(v3), int(v4), int(v5)
for i in(v1,v2,v3,v4,v5):
getUserGuess.append(i)
return getUserGuess
#this function will compare the cpu generated password to the user inputed numbers list
def reportResult(generatePassword,getUserGuess):
correctdigits = 0
correctlocations = 0
global turnsleft #use the play variable initiated outside the funtion
global play #use the play variable initiated outside the funtion
while play is True:
if getUserGuess == generatePassword:
print("Congradulations! You have guessed the right password.")
elif turnsleft == 0:
print("You will never guess my password! It was " +str(generatePassword()))
playagain = input("Would you like to play again? (y/n) ")
if playagain == 'n':
play = False
else:
turnsleft-= 1
for e in getUserGuess():
if e in generatePassword():
correctdigits+= 1
for e in getUserGuess():
if e in generatePassword():
correctlocations+= 1
print(str(turnsleft) +" guesses left.")
print(str(correctdigits) +" of 5 correct digits.")
print(str(correctlocations) +" of 5 correct locations.")
return reportResult
while play is True:
reportResult(generatePassword,getUserGuess)
我相信,当“turnsleft”为0时,只需将“turnsleft”设置为大于0的值。你知道吗
例如:
这将允许你'开始一个新的游戏'的回合设置为一些价值。但它也带来了新的问题。也许您应该编写一个resetGame(),它将编辑所有需要的变量,以便真正从头开始。你知道吗
相关问题 更多 >
编程相关推荐