-4

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)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
PYY
  • 1
  • 2
  • Look at your code again. What variable are you checking to see whether you should display the "want to play again?" message? Maybe if you modify/reset that variable when they type "y" it will stop being displayed... – entrez Oct 22 '18 at 20:44
  • Another problem is that you seem to call the functions to generate the password and to get the user guess multiple times. Every time you call those functions it generates a new password and requests a new guess from the user. The user guess should be called once per turn, stored into a variable, and then that variable should be checked against the password which is generated only once per game. – entrez Oct 22 '18 at 20:48
  • It seems like what you are passing to your function sa such `reportResult(generatePassword,getUserGuess)`, is actually the names of the function objects and not the execution of the function like generatePassword() – LeKhan9 Oct 22 '18 at 20:50
  • And ... it'll be tough for any user to get the code right since you're just generating a random sampling from the int range you specified :/ – LeKhan9 Oct 22 '18 at 20:51

1 Answers1

1

I believe you simply need to set 'turnsleft' to some value greater than 0 in when 'turnsleft' is 0.

For example:

elif turnsleft == 0:
        print("You will never guess my password! It was " +str(generatePassword()))
        turnsleft = 2 #<-- Reset turns here!
        playagain = input("Would you like to play again? (y/n) ")
        if playagain == 'n':
            play = False

This will allow you to 'start a new game' with the turns set to some value. But it also introduces new problems. Perhaps you should write a resetGame() that will edit all the variables it needs to to truly start from scratch.