Search code examples
pythontic-tac-toe

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


I am trying my best to learn Python as I recently switched my major and have become infatuated with the world of Computer Science! I genuinely enjoy it however there are times where like a lot of people do get stuck. I am currently doing a Udemy course and trying to tackle one of the first milestones, however it seems like everything comes out accordingly but... results to error... What I have in mind is not complete yet however I do like to test it from time to time just to make sure everything is going according to plan.

I get the following error yet it still gives me the result... Please if anyone has any tips or suggestions that they can give to a beginner that would be much appreciated! (First Post! Be Nice! :') )

     | | 
     | | 
     | | 
    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 

CODE BELOW

"""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)

Solution

  • display() doesn't return anything, it just prints the board. So the default return value is None.

    But boardUP() ends with return display(gameon_Board), so it's returning that None value.

    And then you assign this result back to the variable with

    gameon_Board = boardUP(gameon_Board,position)
    

    So after the first iteration, gameon_Board no longer contains the list of board elements, it contains None. And then when you try to access its subscripts you get an error.

    Also, you pass the board as an argument to boardUP, but it never uses it; it uses the global variable instead. You should use the parameter.

    Change the function to:

    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