python中的文本自动填充程序与列表indice问题

时间:2015-05-12 19:18:38

标签: python

def printBoard(board):
    for i in board:
        for j in i:
            print(j, end="")
        print()

def autofill(board,square):
    if board[int(square[0])][int(square[1])] == "X":
        pass
    else:
        board[int(square[0])][int(square[1])] = "X"
        if int(square[0]) + 1 < len(board[int(square[0])]):
            squareList2 = [(int(square[0]) + 1), (int(square[1]))]
            board = autofill(board,squareList2)
        if int(square[0]) - 1 >= 0:
            squareList3 = [(int(square[0]) - 1),(int(square[1]))]
            board = autofill(board, squareList3)
        if int(square[1]) + 1 < len(board[int(square[1])]):
            squareList4 = [(int(square[0])),(int(square[1]) + 1)]
            board = autofill(board, squareList4)
        if int(square[1]) - 1 >= 0:
            squareList5 = [(int(square[0])),(int(square[1] - 1))]
            board = autofill(board, squareList5)
    return board

def main():
    board = []
    filename = input("Please enter a filename: ")
    file = open(filename,"r")
    for line in file:
        row = []
        for item in line:
            if item != "\n":
                row.append(item)
        board.append(row)
    printBoard(board)
    square1 = ""
    while square1 != "q":
        square1 = input("Please enter a square to fill, or q to exit: ")
        if square1 != "q":
            squareList = square1.split(", ")
            squareList[0] = int(squareList[0]) - 1
            squareList[1] = int(squareList[1]) - 1
            board = autofill(board, squareList)
            print(len(board))
            print(len(board[0]))
            printBoard(board)

main()

输出

Please enter a filename: input.txt
OOOOOOXOOOO
OOOOOXOOOOO
OOOOXOOOOOO
XXOOXOOOOOO
XXXXOOOOOOO
OOOOOOOOOOO
Please enter a square to fill, or q to exit: 1, 1
6
11
XXXXXXXOOOO
XXXXXXOOOOO
XXXXXOOOOOO
XXXXXOOOOOO
XXXXOOOOOOO
OOOOOOOOOOO
Please enter a square to fill, or q to exit: 6, 1
Traceback (most recent call last):
  File "proj2.py", line 49, in <module>
    main()
  File "proj2.py", line 44, in main
    board = autofill(board, squareList)
  File "proj2.py", line 14, in autofill
    board = autofill(board,squareList2)
  File "proj2.py", line 8, in autofill
    if board[int(square[0])][int(square[1])] == "X":
IndexError: list index out of range

而不是得到错误,6,1应该使一切都成为X.

这是学校的家庭作业,规则是&#34;自动填充需要一个给定的方格,如果是X则什么都不做。如果正方形中有一个O,它会将O更改为X并自动填充上方,下方,左侧和右侧的方块。该程序的示例运行:&#34;

文本文件并不重要,它只是导入2d列表板的方式。

我不明白为什么我会遇到这些列表问题。我打印出行长和列长,它们似乎是正确的。

1 个答案:

答案 0 :(得分:1)

看起来问题是您正在检查是否可以通过检查square[0]的长度来增加board[int(square[0])],而不仅仅是board的长度。 square[1]也是如此 - 您正在检查board[int(square[0])][int(square[1])]而不是board[int(square[0])]。实际上,你检查的程度太深了。

我已经修复了问题,并且明显清理了代码(在我清理完代码之前,我无法修复它)。 autofill()以外的更改标有# note change

def printBoard(board):
    for i in board:
        print(*i, sep='') # note change

def autofill(board,x, y):
    if board[x][y] != "X":
        board[x][y] = "X"
        if x + 1 < len(board):
            board = autofill(board,x+1, y)
        if x - 1 >= 0:
            board = autofill(board, x-1, y)
        if y + 1 < len(board[x]):
            board = autofill(board, x, y+1)
        if y - 1 >= 0:
            board = autofill(board, x, y-1)
    return board

def main():
    filename = input("Please enter a filename: ")
    with open(filename, 'r') as f: # note change
        board = [list(line.strip()) for line in f] # note change
    printBoard(board)
    square1 = ""
    while square1 != "q":
        square1 = input("Please enter a square to fill, or q to exit: ")
        if square1 != "q":
            x, y = map(int, square1.split(",")) # note change
            x-=1 # note change
            y-=1 # note change
            board = autofill(board, x, y) # note change
            printBoard(board)
main()