【问题标题】:(Python) List index out of range(Python) 列表索引超出范围
【发布时间】:2016-01-28 02:13:50
【问题描述】:

所以,在我正在开发的一款井字游戏中,我决定让 AI 成为一个随机数,开个玩笑。具体如下:

  board = []
def createboard():
    board.append(['_',' ','_',' ','_'])
    board.append(['_',' ','_',' ','_'])
    board.append(['_',' ','_',' ','_'])
...
while bot_move_val == False or bot_move_made == False:

        bot_move_row = 0
        bot_move_col = 0

        bot_move_row_rand = randint(0,3) 
        bot_move_col_rand = randint(0,3)

        if bot_move_row_rand == 1:
            bot_move_row = 0
            bot_move_made = True
        elif bot_move_row_rand == 2:
            bot_move_row = 2
            bot_move_made = True
        elif bot_move_row_rand == 3:
            bot_move_row = 4
            bot_move_made = True

        if bot_move_col_rand == 1:
            bot_move_col = 0
            bot_move_made = True
        elif bot_move_col_rand == 2:
            bot_move_col = 2
            bot_move_made = True
        elif bot_move_col_rand == 3:
            bot_move_col = 4
            bot_move_made = True


        if bot_move_row > 4 or bot_move_row < 0 or bot_move_col > 4 or bot_move_col < 0 or board[bot_move_row][bot_move_col] == 'X':
            break
        else:
            bot_move_val = True

代码产生这个错误:

Traceback (most recent call last):
File "C:\Users\[DATA EXPUNGED]\gamestravaganza.py", line 250, in <module>
if bot_move_row > 4 or bot_move_row < 0 or bot_move_col > 4 or bot_move_col < 0 or board[bot_move_row][bot_move_col] == 'X':
IndexError: list index out of range

据我所知,bot_move_rowbot_move_col 不在列表 board 中,尽管它们最多与定义的列表一样长。

【问题讨论】:

  • 不应该是 2 而不是 4 吗?你有 3 行/列:0、1、2。

标签: python list indexing


【解决方案1】:

与 Python 中的其他一些地方(如 range 方法)不同,random.randint(a,b) 包含上限。所以 3 是一个可能的结果,而且由于棋盘列表是零索引的,这将超出范围。

或者如the docs 所说:

random.randint(a, b)
返回一个随机整数 N,使得 a

所以你应该改用randint(0,2)

【讨论】:

  • 哇,我智障了。现在我必须改变整个事情。
  • 也许从头开始并尝试改进您的原始设计。它缺乏优雅。
【解决方案2】:

您只有 3 行。 if 条件应该是

if bot_move_row > 2 or bot_move_row < 0 or bot_move_col > 4 or bot_move_col < 0 or board[bot_move_row][bot_move_col] == 'X':
        break

【讨论】:

    【解决方案3】:

    假设您在某个时候调用函数createboard 来构建您的电路板,那么有几个地方会造成困难。

    您创建的板有 3 行和 5 列,但您以相同的方式翻译行和列(即预期行数与列数相等)。我会让它们在 3 点保持不变。

    如其他地方所述,您对 randint 的调用从 4 个数字(0、1、2、3)的范围中选择,因为 randint 包括两个端点。

    最后,您的 if 条件测试值是否在您自己指定的范围内,从而不太清楚您真正测试的是什么。

    要获得与您在上面似乎想要的相同的基本功能,您可以将其重写为:

    # Create your board
    board = [['_','_','_'], ['_','_','_'], ['_','_','_']]
    
    #Handle one move
    def botMove():
        while False:
            # Attempt to choose a row/column
            row = randint(0, 2)
            column = randint(0, 2)
            if board[row][column] == '_':
                # Randomly selected row/column is Empty, mark it as the bots and return
                board[row][column] = 'X'
                break
    

    这当然不能处理板满时发生的情况(如上所述,它会无限循环)。要充实游戏,您需要一些东西来处理用户输入(假设玩家与计算机)和一些东西来确定获胜者/平局。

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多