My for loop won't break, even when the if statement condition is true

29 Views Asked by At

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.

0

There are 0 best solutions below