gpt4 book ai didi

python - python中的文本自动填充程序存在列表索引问题

转载 作者:太空宇宙 更新时间:2023-11-03 17:43:03 25 4
gpt4 key购买 nike

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,而不是出现错误。

这是一个学校作业,规则是“自动填充一个给定的正方形,如果它是 X,则不执行任何操作。如果该正方形中有 O,则将 O 更改为 X 并自动填充上面的正方形、下方、左侧和右侧。程序运行示例:"

文本文件并不重要,重要的是二维列表板的导入方式。

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

最佳答案

看起来问题是您正在通过检查 board[int(square[0])] 的长度来检查是否可以增加 square[0] > 而不仅仅是的长度。与 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()

关于python - python中的文本自动填充程序存在列表索引问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com