2

I am new to python. I've been trying to make a very basic console version of the popular game Wordle. As you only get 6 lives, I've been using a for loop which iterates 6 times as my game loop. (I've used a dodgy method using that string, I know).

def startGame():
    iterations = "123456"
    for i in iterations:
        guess = getGuess()
        checkWord(guess)
        analyseString(guess, answer)
        if endGame == True:
            print(endGame)
            break

Here, you can see, I've tried to break the loop when the if statement evaluates to true. I believe the problem is in this if statement, as endGame isn't being printed. endGame is set to true by the stop() function:

def stop():
    endGame = True
    return endGame

Which is called by the analyseString() function when the user's guess is the same string as the answer.

def analyseString(string, answer):
    print("\n")
    length = 5
    for i in range(length):
        if string == answer:
            print("Congratulations! You guessed the word! Thanks for playing.")
            stop()
            break
            
        elif string[i] == answer[i]:
            print(string[i], "     Green")
        else:
            print(string[i], "     Grey")

I know this function works correctly because the congratulations string is printed, but for some reason, when I win the game, the for loop carries on iterating and the getGuess() function is once again called.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    because you are setting a local scoped variable `endGame = True` inside stop(). return its value but never do anything with the result of `stop()` inside `analysysString` - and also never modify the global `endGame` you seem to use inside `startGame` – Patrick Artner Feb 14 '22 at 01:34
  • 1
    remove the global variable and use return's: `if analyseString(guess, answer): break` and inside analyseString instead of `break` do `return True` – Patrick Artner Feb 14 '22 at 01:36
  • Awesome. Thank you. I guess I need to change the way I think about variables and return statements – ginge_george Feb 14 '22 at 01:41

0 Answers0