0

I am building a tic tac toe game in python. it is very unpolished at this stage, but functional. my issue is this - I made a function to check for winners. it looks like this:

def check_winner(spots):
        if spots[1] == spots[2] == spots[3]:
            return True
            print("Winner found!")
        if spots[4] == spots[5] == spots[6]:
            return True
            print("Winner found!")
        if spots[7] == spots[8] == spots[9]:
            return True
            print("Winner found!")
        if spots[1] == spots[4] == spots[7]:
            return True
            print("Winner found!")
        if spots[3] == spots[6] == spots[9]:
            return True
            print("Winner found!")
        if spots[1] == spots[5] == spots[9]:
            return True
            print("Diagonal winner found!")
        if spots[3] == spots[5] == spots[7]:
            return True
            print("Diagonal winner found!")

I have my check_winner function in my while loop for the game below. However, when a winner is found on the board, and the function returns true, the "winner found" statement is not being printed at all. why is this?:

    while winner == False:
        if Turns % 2:
            Letter = 'X'
        else:
            Letter = 'O'
        print_board(spots)
        if check_winner(spots) == True:
            break
  • Because your print statements all come AFTER your return statements. Things happen in order. By the way, Python starts counting its arrays at 0, not at 1. You almost certainly want `spots[0] == spots[1] == spots[2]`, not 1, 2, 3. – Tim Roberts Sep 01 '22 at 03:48
  • @TimRoberts thank you. I can't believe I made such a simple error. by the way, when I call spots, I am referring to a dictionary, so I believe everything is working well. my board is drawn by referring to this dictionary. spots = { 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9' } – rooknoobski Sep 01 '22 at 03:50
  • Please see the linked duplicate to clear up underlying misunderstandings. `print` and `return` have **nothing to do with each other**; it is only possible to `return` from a function once per time it is called (and nothing else in the function happens after that; and this code does not return strings at all. – Karl Knechtel Sep 01 '22 at 03:56
  • 2
    As an aside, please keep in mind that functions are supposed to do one thing. Rather than trying to fix the `print`s inside the function, get rid of them, and let the *calling code* `print` a result before the `break`. Telling the user that there was a winner, is a **different task** from figuring out whether that's true. – Karl Knechtel Sep 01 '22 at 03:57

1 Answers1

0

you have to move all the prints before the return, because the return terminates the function, and all the instructions below are not executed.

def check_winner(spots):
    if spots[1] == spots[2] == spots[3]:
        print("Winner found!")
        return True
    if spots[4] == spots[5] == spots[6]:
        print("Winner found!")
        return True
    if spots[7] == spots[8] == spots[9]:
        print("Winner found!")
        return True
    if spots[1] == spots[4] == spots[7]:
        print("Winner found!")
        return True
    if spots[3] == spots[6] == spots[9]:
        print("Winner found!")
        return True
    if spots[1] == spots[5] == spots[9]:
        print("Diagonal winner found!")
        return True
    if spots[3] == spots[5] == spots[7]:
        print("Diagonal winner found!")
        return True
Likor
  • 26
  • 3