Python 分配给列表后出现元组索引超出范围错误

Python Tuple Index Out of Range error after assignment to list

不久前我参加了一些 Python 课程,并且已经完成了一个里程碑项目,制作了一个简单的井字游戏。

但由于不断发生的索引错误,我 运行 陷入困境,我无法弄清楚原因。

代码如下:


    #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()

我得到的错误是:

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)

我认为正在发生的事情是在 insert_choice 函数的赋值过程中:

game_list[position] = 'X' 

列表以某种方式转换为元组,变量被附加而不是分配,然后当尝试再次显示列表时它只有两个元素导致索引错误,但我无法弄清楚/为什么/ .

希望有人能帮忙

此致,

insert_choice() 方法 return 是您的 game_listposition 的元组:

return (game_list, position)

因此,在主循环期间,您将此元组存储为新 game_list 并尝试访问大于 1 的索引,这会导致此索引错误。

您可以只 return game_list 或将 returned 元组解压为:

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

由于您没有更改 position 的值,您可能想更改前者。