Home > Enterprise >  Python Tuple Index Out of Range error after assignment to list
Python Tuple Index Out of Range error after assignment to list

Time:05-05

I jumped into some Python courses a little while ago and have gotten to a milestone project to make a simple tic-tac-toe game.

But I am running into a bit of a wall due to an index error that keeps happening and I cannot figure out why.

The code is the following:


    #Tic Tac Toe
    game_list = [' '] * 10
    turn_counter = 0
    game_on = True
    
    def show_game(game_list): 
    
        print('   |   |')
        print(' '   game_list[7]   ' | '   game_list[8]   ' | '   game_list[9])
        print('   |   |')
        print('-----------')
        print('   |   |')
        print(' '   game_list[4]   ' | '   game_list[5]   ' | '   game_list[6])
        print('   |   |')
        print('-----------')
        print('   |   |')
        print(' '   game_list[1]   ' | '   game_list[2]   ' | '   game_list[3])
        print('   |   |')
    
    def choose_position():
        
        # Initial Variables
        within_range = False
        acceptable_values = [1,2,3,4,5,6,7,8,9]
        choice = 'WRONG'
    
        # While loop that keeps asking for input
        while choice.isdigit() == False or within_range == False:
        
            choice = input("Please choose a number between 1-9 like a numpad: ")
        
        # DIGIT CHECK
            if choice.isdigit() == False:
                print("Sorry, that is not a digit!")
                
        # RANGE CHECK
            if choice.isdigit() == True:
                if int(choice) in acceptable_values:
                    within_range = True
                else:
                    print("Sorry, you are out of the acceptable range (1-9)")
                    
        return int(choice)
    
    def insert_choice(game_list, position, turn_counter):
        print(type(position))
        print(position)
        # Place the character in the game_list
        if turn_counter%2 == 0 or turn_counter == 0:
            game_list[position] = 'X'
        else:
            game_list[position] = 'O'
    
        return (game_list, position)
    
    def gameon_choice():
        choice = 'wrong'
        while choice not in ['Y', 'N']:
            choice = input("Keep playing? (Y or N) ")
            
            if choice not in ['Y', 'N', 'R']:
                print("sorry, I don't understand, please choose Y or N ")
        
        if choice == 'Y':
            return True
        else:
            return False
    
    while game_on:
        
        show_game(game_list)
        
        position = choose_position()
    
        game_list = insert_choice(game_list,position,turn_counter)
        
        turn_counter  = turn_counter
    
        show_game(game_list)
        
        game_on = gameon_choice()

And the error I get is:

Exception has occurred: IndexError
tuple index out of range
  File "Desktop/Tictactoe.py", line 9, in show_game
    print(' '   game_list[7]   ' | '   game_list[8]   ' | '   game_list[9])
  File "Desktop/Tictactoe.py", line 79, in <module>
    show_game(game_list)

What I think is happening is that during the assignment in the insert_choice function:

game_list[position] = 'X' 

the list is somehow converted to a tuple and the variables are appended instead of assigned, and then when trying to display the list again it only has two elements leading to an index error, but I cannot figure out /why/.

I hope someone can help.

Sincerely,

CodePudding user response:

The insert_choice() method returns a tuple of your game_list and position:

return (game_list, position)

Thus, during your main loop, you store this tuple as the new game_list and try to access indices greater than 1 which leads to this index error.

You can either only return game_list or unpack the returned tuple as:

game_list, position = insert_choice(game_list,position,turn_counter)

Since you don't change the value of position, you probably want to do the former.

  • Related