简体   繁体   English

TypeError:无法解压不可迭代的 NoneType 对象错误

[英]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.在第 78 行,它讨论了如何存在 Nonetype 值,即使我无论如何都为这两个变量分配了一个值。

Here is a link to a similar problem (I think): [https://stackoverflow.com/questions/53680913/typeerror-cannot-unpack-non-iterable-nonetype-object][1]这是一个类似问题的链接(我认为):[https://stackoverflow.com/questions/53680913/typeerror-cannot-unpack-non-iterable-nonetype-object][1]

Here is where I think the faulty code lies:这是我认为错误代码所在的地方:

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.非常感谢,如果解决方案是一个小错字,我很抱歉,我对此真的很陌生,并且在这方面花费了太多时间。 :D :D

There are certain cases where you don't return anything in the function check_win .在某些情况下,您不会在函数check_win中返回任何内容。

For example, when turn == 1 , you have an if , elif , elif block within that, but nothing for else .例如,当turn == 1时,其中有一个ifelifelif块,但else没有。 So, when turn == 1 , if none of the if statements within the block for that are true, your function will return nothing.因此,当turn == 1时,如果块中没有任何if语句为真,则您的函数将不返回任何内容。 There is a similar problem for turn == 2 . 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.您可能希望将该函数的else块中的代码添加为turn == 1turn == 2else块,因为这将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PyAutoGui TypeError:无法解压不可迭代的NoneType object - PyAutoGui TypeError: cannot unpack non-iterable NoneType object -- TypeError: cannot unpack non-iterable NoneType object - -- TypeError: cannot unpack non-iterable NoneType object Numpy 抛出 TypeError: cannot unpack non-iterable NoneType object - Numpy throwing TypeError: cannot unpack non-iterable NoneType object 方法 Euler TypeError: cannot unpack non-iterable NoneType object - Method Euler TypeError: cannot unpack non-iterable NoneType object TypeError:无法解压不可迭代的 NoneType 对象 - TypeError: cannot unpack non-iterable NoneType object Pyautogui Image 工作一段时间后不可迭代,TypeError: cannot unpack non-iterable NoneType object - Pyautogui Image non-iterable after working for a while, TypeError: cannot unpack non-iterable NoneType object Python 错误 - 无法解压不可迭代的 NoneType object - Python error- cannot unpack non-iterable NoneType object 在终端“TypeError: cannot unpack non-iterable NoneType object”中运行代码发生错误 - Error occurs running code in terminal “TypeError: cannot unpack non-iterable NoneType object” 类型错误:无法解压不可迭代的 object - TypeError: cannot unpack non-iterable object 得到一个错误 TypeError: cannot unpack non-iterable float object - Getting an error TypeError: cannot unpack non-iterable float object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM