Learning Python - TypeError: 'NoneType' object is not subscriptable

Learning Python - TypeError: 'NoneType' object is not subscriptable

我正在努力学习 Python 因为我最近转了专业并且迷上了计算机科学的世界!我真的很喜欢它,但有时很多人确实会被卡住。我目前正在上 Udemy 课程并试图解决第一个里程碑,但是似乎一切都相应地出现了但是......结果出错......我想到的还没有完成但是我喜欢测试只是为了确保一切都按计划进行。

我收到以下错误,但它仍然给我结果... 如果有人可以给初学者任何提示或建议,我们将不胜感激! (第一个 Post!做个好人!:'))

     | | 
     | | 
     | | 
    Player 1 are you X or O? X
    Please Input a Number 1-9: 6
     | | 
     | |O
     | | 
    Traceback (most recent call last):
      File "C:/Users/Danny/Desktop/Python/Python Bootcamp/Milestones/TicTacToe.py", line 76, in <module>
        display(gameon_Board)
      File "C:/Users/Danny/Desktop/Python/Python Bootcamp/Milestones/TicTacToe.py", line 17, in display
        print(board[7]+'|'+board[8]+'|'+board[9])
    TypeError: 'NoneType' object is not subscriptable 

代码如下

"""This is a TIC TAC TOE GAME!
    
You need to do the following...

We need to print a board.
Take in player input.
Place their input on the board.
Check if the game is won,tied, lost, or ongoing.
Repeat c and d until the game has been won or tied.
Ask if players want to play again.

Good Luck! """

#Display Board

def display(board):
    print(board[7]+'|'+board[8]+'|'+board[9])
    print(board[4]+'|'+board[5]+'|'+board[6])
    print(board[1]+'|'+board[2]+'|'+board[3])

#Sets up player markers
def player_Marker():
    marker = ' '
    while marker != 'X' and marker != 'O':
        marker = input("Player 1 are you X or O? ")
        
    player1 = marker
    
    if player1 == 'X':
        player2 = 'O'
    else:
        player2 = 'X'
    
    return (player1,player2)

#Takes User Position Input On Board
def player_Choice():
    position = 'wrong'
    while position not in range(1,10):
        position = int(input("Please Input a Number 1-9: "))  
    
    return position

#Tack on Inputs to Board

# CLEAN_Board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']

gameon_Board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']

def boardUP(board,position):
    turn = 1
    if turn == 1:
        gameon_Board[position] = player1_Marker
        turn = turn - 1
    if turn == 0:
        gameon_Board[position] = player2_Marker
        turn += 1

    return display(gameon_Board)


# All Together

game_on = True
gameon_Board = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ']

while game_on:
    display(gameon_Board)
    
    player1_Marker , player2_Marker = player_Marker()
    
    position = player_Choice()

    gameon_Board = boardUP(gameon_Board,position)

    display(gameon_Board)

display() 没有 return 任何东西,它只是打印电路板。所以默认的 return 值为 None.

但是 boardUP()return display(gameon_Board) 结尾,所以 return 是 None 值。

然后你将这个结果赋值给变量

gameon_Board = boardUP(gameon_Board,position)

所以在第一次迭代之后,gameon_Board 不再包含棋盘元素列表,它包含 None。然后当你尝试访问它的下标时你会得到一个错误。

此外,您将棋盘作为参数传递给 boardUP,但它从不使用它;它改用全局变量。您应该使用参数。

将函数更改为:

def boardUP(board,position):
    turn = 1
    if turn == 1:
        board[position] = player1_Marker
        turn = turn - 1
    if turn == 0:
        board[position] = player2_Marker
        turn += 1

    display(board)
    return board