簡體   English   中英

Python中的IndexError,我不明白為什么

[英]IndexError in Python and I don't understand why

Python 3.7 Windows 7

我正在創建“ Connect 4”程序,但在編輯列表值時遇到問題。

我的代碼:不要關注doctest,因為我沒有更新它們以顯示更改

def get_piece(board, row, column):
    """Returns the piece at location (row, column) in the board.

    >>> rows, columns = 2, 2
    >>> board = create_board(rows, columns)
    >>> board = put_piece(board, rows, 0, 'X')[1] # Puts piece "X" in column 0 of board and updates board
    >>> board = put_piece(board, rows, 0, 'O')[1] # Puts piece "O" in column 0 of board and updates board
    >>> get_piece(board, 1, 0)
    'X'
    >>> get_piece(board, 1, 1)
    '-'
    """
    breakloop = False
    if row < 0:
        breakloop = True
        return breakloop
    return row, board[row][column]

def create_board(rows, columns):
    """Returns a board with the given dimensions.

    >>> create_board(3, 5)
    [['-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-']]
    """
    return [create_row(columns) for i in range(rows)]

def make_move(board, col, player):
    """Put player's piece in column COL of the board, if it is a valid move.

    """
    assert col <= len(board[1]) and not col < 0, "Move must be bewteen 0 and " + len(board[1])

    row, piece = get_piece(board, len(board[1]), col)
    while piece == "X" or piece == "Y":
        row, piece = get_piece(board, len(board[1])-1, col)
        if breakloop:
            assert "That column is full"
    return replace_elem(board, row, col, player)

def replace_elem(board, row, col, elem):
    """Create and return a new list whose elements are the same as those in
    LST except at index INDEX, which should contain element ELEM instead.

    >>> old = [1, 2, 3, 4, 5, 6, 7]
    >>> new = replace_elem(old, 2, 8)
    >>> new
    [1, 2, 8, 4, 5, 6, 7]
    >>> new is old   # check that replace_elem outputs a new list
    False
    """

    board[row][column] = elem
    return board

def create_row(size):
    """Returns a single, empty row with the given size. Each empty spot is
    represented by the string '-'.

    >>> create_row(5)
    ['-', '-', '-', '-', '-']
    """
    return  ['-' for i in range(size)]

當我使用len(board [0])或len(board [1])時,它給了我一個預期的數字。 當我使用len調用get_board時,它給了我:

 Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    make_move(z, 2, "X")
  File "C:\Users\comp\Documents\cs61a\Practice.py", line 54, in make_move
    row, piece = get_piece(board, len(board[1]), col)
  File "C:\Users\comp\Documents\cs61a\Practice.py", line 17, in get_piece
    return row, board[row][column]
IndexError: list index out of range

但是當我直接使用數字時,效果很好。

簡單地說,就像Python所說的那樣,您的make_move()函數調用get_piece()時,其行或列的值超出了索引的允許范圍。 特別是函數的第七行,您在此處調用第一時間get_piece() 您在len(board[1])附近忘記了-1因此您傳遞的是列表的長度5,因此當您嘗試讀取它時,Python會引發錯誤。

row, piece = get_piece(board, len(board[1]), col) #errata
row, piece = get_piece(board, len(board[1]) - 1, col) #correct

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM