This is code I wrote, it runs perfectly and so does the function this code is from except for one part, when it comes to the part where I ask the user if they want to try again, if they select no then it stops which is what is suppose to happen. On the other hand, if they say yes ‘y’ they will get another exact same prompt 'Would you like to try again? (y/n) '. You can say y 100 times and nothing will happen, my goal is for the code to go back and call the function from the start. I have tried a break if the user says ‘y’ to try and get out of the loop etc but it did not work and I now have no idea…
Additionally, as you can see, I have correct digits which compares to see if the user guessed number is in the generated list, that part I have no problem with. Now with the correct locations, I am not sure how to do that, the goal is to check is both the number and location is the name in both lists.
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)