TypeError:“ NoneType”对象不可调用,但对象不是nonetype

时间:2018-08-24 08:43:35

标签: python python-3.x

def winner(board):
    WAYS_TOWIN = ((0, 1, 2),
                  (3, 4, 5),
                  (6, 7, 8),
                  (0, 4, 8),
                  (2, 4, 6),
                  (0, 3, 6),
                  (1, 4, 7),
                  (2, 5, 8))
    for row in WAYS_TOWIN:
        if board[row[0]] == board[row[1]] == board[row[2]] != " ":
            winner = board[row[0]]
            return winner
    if " " not in board:
            return TIE
    else:
        return None



#Main
instructions()
human = input("Enter your name: "); print("\n")
pieces = who_first(human, computer); print("\n")#pieces becomes a list with human piece first and computer piece second
board = new_board(); print("\n")
winner = winner(board)



while winner == None and winner != TIE:
    if turn == pieces[0]:#if human is first
        winner = winner(board)
  

当我第一次运行Winner函数时,它没有返回到Winner变量,也没有错误。但是当我第二次运行它时,它给我TypeError:'NoneType'对象不可调用。板子是带有[“”] * 9的列表,我不明白为什么第二次调用板子是无类型对象。

1 个答案:

答案 0 :(得分:1)

您第一次运行function redirect($url){ $string = '<script type="text/javascript">'; $string .= 'window.location = "' . $url . '"'; $string .= '</script>'; echo $string; } 获胜者会变成winner = winner(board),因此在第二次调用时它不是在调用函数,而是在None时出现了错误。更改名称将解决问题,不建议使用相同的变量名和函数名。