Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: cannot unpack non-iterable NoneType object Error

I looked at the other answers but they didn’t seem to work for me, or I could just be doing something wrong, or not understanding what I am supposed to do in the context of this code. On line 78, it talks about how there is a Nonetype value, even though I assigned a value to both variables no matter what.

Here is a link to a similar problem (I think):
[https://stackoverflow.com/questions/53680913/typeerror-cannot-unpack-non-iterable-nonetype-object][1]

Here is where I think the faulty code lies:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def check_win(turn, gamestate):
    if turn == 1:
        if gamestate[0] == gamestate[1] == gamestate[2] != '-' or gamestate[3] == gamestate[4] == gamestate[5] != '-' or gamestate[6] == gamestate[7] == gamestate[8] != '-':
            winner = 1
            win = True
            return winner, win
        elif gamestate[0] == gamestate[3] == gamestate[6] != '-' or gamestate[1] == gamestate[4] == gamestate[7] != '-' or gamestate[2] == gamestate[5] == gamestate[8] != '-':
            winner = 1
            win = True
            return winner, win
        elif gamestate[0] == gamestate[4] == gamestate[8] != '-' or gamestate[2] == gamestate[4] == gamestate[6] != '-':
            winner = 1
            win = True
            return winner, win
    elif turn == 2:
        if gamestate[0] == gamestate[1] == gamestate[2] or gamestate[3] == gamestate[4] == gamestate[5] or gamestate[6] == gamestate[7] == gamestate[8]:
            winner = 2
            win = True
            return winner, win
        elif gamestate[0] == gamestate[3] == gamestate[6] or gamestate[1] == gamestate[4] == gamestate[7] or gamestate[2] == gamestate[5] == gamestate[8]:
            winner = 2
            win = True
            return winner, win
        elif gamestate[0] == gamestate[4] == gamestate[8] or gamestate[2] == gamestate[4] == gamestate[6]:
            winner = 2
            win = True
            return winner, win
    else:
        winner = 'None' 
        win = False
        return winner, win

Later on when I try to alter those values in another function:

winner, win = check_win(turn, gamestate) <--- Where error was found

Here is the code in its entirety :

print('Tic Tac Toe:')
pos = 0
turn = 1
gamestate = ['-', '-', '-', '-', '-', '-', '-', '-', '-']

def display_board(gamestate):
    print(gamestate[0:3])
    print(gamestate[3:6])
    print(gamestate[6:9])
def move_validity(pos, gamestate):
    if str(gamestate[int(pos)]) != '-':
        print('Invalid move.')
        valid = False
        return valid
    else:
        valid = True
        return valid
def update(gamestate, pos):
        if turn == 1:
            gamestate[int(pos)] = 'X'
        if turn == 2:
            gamestate[int(pos)] = 'O'
        else:
            print('ERROR')
def check_win(turn, gamestate):
    if turn == 1:
        if gamestate[0] == gamestate[1] == gamestate[2] != '-' or gamestate[3] == gamestate[4] == gamestate[5] != '-' or gamestate[6] == gamestate[7] == gamestate[8] != '-':
            winner = 1
            win = True
            return winner, win
        elif gamestate[0] == gamestate[3] == gamestate[6] != '-' or gamestate[1] == gamestate[4] == gamestate[7] != '-' or gamestate[2] == gamestate[5] == gamestate[8] != '-':
            winner = 1
            win = True
            return winner, win
        elif gamestate[0] == gamestate[4] == gamestate[8] != '-' or gamestate[2] == gamestate[4] == gamestate[6] != '-':
            winner = 1
            win = True
            return winner, win
    elif turn == 2:
        if gamestate[0] == gamestate[1] == gamestate[2] or gamestate[3] == gamestate[4] == gamestate[5] or gamestate[6] == gamestate[7] == gamestate[8]:
            winner = 2
            win = True
            return winner, win
        elif gamestate[0] == gamestate[3] == gamestate[6] or gamestate[1] == gamestate[4] == gamestate[7] or gamestate[2] == gamestate[5] == gamestate[8]:
            winner = 2
            win = True
            return winner, win
        elif gamestate[0] == gamestate[4] == gamestate[8] or gamestate[2] == gamestate[4] == gamestate[6]:
            winner = 2
            win = True
            return winner, win
    else:
        winner = 'None'
        win = False
        return winner, win
def restart():
    gamestate = ['-', '-', '-', '-', '-', '-', '-', '-', '-']
    turn = 1
    win = False
    return gamestate, turn, win
def choose_position():
    pos = input('Player ' + str(turn) + ': ')
    if int(pos) < 0 or int(pos) > 8:
        print('Invalid move')
    else:
        return pos
def Tic_Tac_Toe():
    while True:
        global pos
        global turn
        if turn == 1:
            pos = choose_position()
            valid = move_validity(pos, gamestate)
            if valid == True:
                update(gamestate, pos)
            if valid == False:
                break
            winner, win = check_win(turn, gamestate)
            if win == True:
                print(str(winner) + ' wins!')
                break
            if '-' not in gamestate:
                print('Tie game.')
                break
        if turn == 2:
            pos = choose_position()
            if move_validity(pos, gamestate) == True:
                continue
            if move_validity(pos, gamestate) == False:
                break
            update(gamestate, pos)
            winner, win = check_win(turn, gamestate)
            if win == True:
                print(str(winner) + ' wins!')
                break
            turn = 1
            if '-' not in gamestate:
                print('Tie game.')
                break

        display_board(gamestate)
display_board(gamestate)
Tic_Tac_Toe()
restart_case = input('y/n Would you like to play? ')
if restart_case == 'y':
    gameboard, turn, win = restart()

Thank you so much, and sorry if the solution was a small typo, I am really new to this and have spent way too much time on this. 😀

>Solution :

There are certain cases where you don’t return anything in the function check_win.

For example, when turn == 1, you have an if, elif, elif block within that, but nothing for else. So, when turn == 1, if none of the if statements within the block for that are true, your function will return nothing. There is a similar problem for turn == 2. You may want to add the code you have in the else block for that function as an else block for both turn == 1 and turn == 2, as this will fix your problem.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading