从 if 语句中删除 IndexError - 迷宫解决软件
Remove IndexError from if statement - Maze solving Software
所以我想编写一个解决迷宫的程序,但导入迷宫已经失败了。这是我的代码:
def import_maze(filename):
temp = open(filename, 'r')
x, y = temp.readline().split(" ")
maze = [[0 for x in range(int(y))] for x in range(int(x))]
local_counter, counter, startx, starty = 0, 0, 0, 0
temp.readline()
with open(filename) as file:
maze = [[letter for letter in list(line)] for line in file]
for i in range(1, int(y)):
for z in range(0, int(x)):
if maze[i][z] == '#':
local_counter += 1
if local_counter < 2 and maze[i][z] == " ":
counter += 1
if maze[i][z] == 'K':
startx, starty = i, z
local_counter = 0
return maze, startx, starty, counter
maze, startx, starty, counter = import_maze("kassiopeia0.txt")
print(counter, "\n", startx, ":", starty, "\n", maze)
稍微解释一下:local_counter 是"showing" 迷宫的边界。所以我可以计算数组中的空白元素。它们的数量将保存在柜台中,我需要作为回避依据。
我收到的错误消息是:
C:\Python34\python.exe C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py
Traceback (most recent call last):
File "C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py", line 27, in <module>
maze, startx, starty, counter = import_maze("kassiopeia0.txt")
File "C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py", line 16, in import_maze
if maze[i][z] == '#':
IndexError: list index out of range
Process finished with exit code 1
最后是 kassiopeia0.txt-文件:
6 9
#########
# # #
# # # #
# K # #
# # #
#########
对不起我的英语。
您在 kassiopeia0.txt 的 header 行中指定了一个 6×9 迷宫,但文件的其余部分包含一个 9×6 迷宫。
交换 6 和 9,迷宫应该可以正常显示。它对我有用。
@卢克是对的。我建议您使用以下代码:
def import_maze(filename):
with open(filename) as f:
maze = [[letter for letter in line.strip()] for line in f.readlines() if line.strip()]
local_counter, counter, startx, starty = 0, 0, 0, 0
for y, row in enumerate(maze):
for x, cell in enumerate(row):
if cell == '#':
local_counter += 1
elif local_counter < 2 and cell == ' ':
counter += 1
elif cell == 'K':
startx, starty = x, y
local_counter = 0
return maze, startx, starty, counter
您的文件是:
#########
# # #
# # # #
# K # #
# # #
#########
所以我想编写一个解决迷宫的程序,但导入迷宫已经失败了。这是我的代码:
def import_maze(filename):
temp = open(filename, 'r')
x, y = temp.readline().split(" ")
maze = [[0 for x in range(int(y))] for x in range(int(x))]
local_counter, counter, startx, starty = 0, 0, 0, 0
temp.readline()
with open(filename) as file:
maze = [[letter for letter in list(line)] for line in file]
for i in range(1, int(y)):
for z in range(0, int(x)):
if maze[i][z] == '#':
local_counter += 1
if local_counter < 2 and maze[i][z] == " ":
counter += 1
if maze[i][z] == 'K':
startx, starty = i, z
local_counter = 0
return maze, startx, starty, counter
maze, startx, starty, counter = import_maze("kassiopeia0.txt")
print(counter, "\n", startx, ":", starty, "\n", maze)
稍微解释一下:local_counter 是"showing" 迷宫的边界。所以我可以计算数组中的空白元素。它们的数量将保存在柜台中,我需要作为回避依据。 我收到的错误消息是:
C:\Python34\python.exe C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py
Traceback (most recent call last):
File "C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py", line 27, in <module>
maze, startx, starty, counter = import_maze("kassiopeia0.txt")
File "C:/Users/Anton/PycharmProjects/BWINF_Aufgabe_1/Wegfinden.py", line 16, in import_maze
if maze[i][z] == '#':
IndexError: list index out of range
Process finished with exit code 1
最后是 kassiopeia0.txt-文件:
6 9
#########
# # #
# # # #
# K # #
# # #
#########
对不起我的英语。
您在 kassiopeia0.txt 的 header 行中指定了一个 6×9 迷宫,但文件的其余部分包含一个 9×6 迷宫。
交换 6 和 9,迷宫应该可以正常显示。它对我有用。
@卢克是对的。我建议您使用以下代码:
def import_maze(filename):
with open(filename) as f:
maze = [[letter for letter in line.strip()] for line in f.readlines() if line.strip()]
local_counter, counter, startx, starty = 0, 0, 0, 0
for y, row in enumerate(maze):
for x, cell in enumerate(row):
if cell == '#':
local_counter += 1
elif local_counter < 2 and cell == ' ':
counter += 1
elif cell == 'K':
startx, starty = x, y
local_counter = 0
return maze, startx, starty, counter
您的文件是:
#########
# # #
# # # #
# K # #
# # #
#########