我有一个编译错误“未定义”,尽管有定义
from gasp import *
GRID_SIZE = 30
MARGIN = GRID_SIZE
BACKGROUND_COLOR = color.BLACK # Colors we use
WALL_COLOR = (0.6 * 255, 0.9 * 255, 0.9 * 255)
# The shape of the maze. Each character
# represents a different type of object
# % - Wall
# . - Food
# o - Capsule
# G - Ghost
# P - Chomp
# Other characters are ignored
the_layout = [
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
"%.....%.................%.....%",
"%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
"%.%.....%......%......%.....%.%",
"%...%%%.%.%%%%.%.%%%%.%.%%%...%",
"%%%.%...%.%.........%.%...%.%%%",
"%...%.%%%.%.%%% %%%.%.%%%.%...%",
"%.%%%.......%GG GG%.......%%%.%",
"%...%.%%%.%.%%%%%%%.%.%%%.%...%",
"%%%.%...%.%.........%.%...%.%%%",
"%...%%%.%.%%%%.%.%%%%.%.%%%...%",
"%.%.....%......%......%.....%.%",
"%o%%%.%.%%%.%%%%%%%.%%%.%.%%%o%",
"%.....%........P........%.....%",
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"]
class Immovable:
def is_a_wall(self):
return False
class Nothing(Immovable):
pass
class Maze:
def __init__(self):
self.have_window = False
self.game_over = False
self.set_layout(the_layout)
set_speed(20)
def set_layout(self, layout):
height = len(layout)
width = len(layout[0])
self.make_window(width, height)
self.make_map(width, height)
max_y = height - 1
for x in range( width ):
for y in range(height):
char = layout[max_y - y][x]
self.make_object((x, y), char)
def make_window(self, width, height):
grid_width = (width -1) * GRID_SIZE
grid_height = (height - 1) * GRID_SIZE
screen_width = 2 * MARGIN + grid_width
screen_height = 2 * MARGIN + grid_height
begin_graphics(screen_width, screen_height,"Chomp",BACKGROUND_COLOR)
def to_screen(self, point):
(x,y) = point
x = x * GRID_SIZE + MARGIN
y = y * GRID_SIZE + MARGIN
return(x,y)
def make_map(self, width, height):
self.width = width
self.height = height
self.map = []
for y in range(width):
new_row = []
for x in range(width):
new_row.append(Nothing())
self.map.append(new_row)
def make_object(self,point,charactor):
(x,y) = point
if charactor == "%":
self.map[y][x] = Wall(self,point)
def finished(self):
return self.game_over
def play(self):
update_when('next_tick')
def done(self):
end_graphics()
self.map = []
def object_at(self,point):
(x,y) = point
if y < 0 or y >= self.height:
return Nothing()
if x < 0 or x >= self.width:
return Nothing()
return self.map[y][x]
class Wall(Immovable):
def __init__(self, maze, point):
self.place = point # Store our position
self.screen_point = maze.to_screen(point)
self.maze = maze # Keep hold of Maze
self.draw()
def draw(self):
(screen_x, screen_y) = self.screen_point
dot_size = GRID_SIZE * 0.2
Circle(self.screen_point, dot_size,
color = WALL_COLOR, filled = 1)
(x, y) = self.place
neighbors = [ (x+1, y), (x-1, y)]
for neighbor in neighbors:
self.check_neighbor(neighbor)
def check_neighbor(self,neighbor):
maze = self.maze
object = maze.object_at(neighbor)
if object.is_a_wall():
here = self.screen_point
there = maze.to_screen(neighbor)
Line(here, there, color = WALL_COLOR,thickness = 2)
def is_a_wall(self):
return True
the_maze = Maze()
while not the_maze.finished():
the_maze.play()
the_maze.done()
我遇到了这个错误..
Traceback (most recent call last):
File "chomp.py", line 110, in class Wall(Immovable):
File "chomp.py", line 124, in Wall for neighbor in neighbors:
NameError: name 'neighbors' is not defined
我花了很多时间还是找不到问题出在哪里,需要一些帮助。
2 个回答
1
我一下子看不出哪里出错了。你最近有没有移动过文件,导致某个导入的代码其实没有运行你新的代码,而是运行了一个.pyc文件?比如说,你最近是不是引入了一个和某个Python文件同名的包?
. \- main.py Has an "import stuff" \- stuff.py This is the code you think is being run. \- stuff \- __init__.py This code is being run.
1
你提到的未完成的 Circle()
调用,可能是因为在格式化代码时出现了错误。请确认你发布的代码是你实际运行的代码,并且错误信息是你真实得到的(这里面有一些信息缺失!)。
在错误信息中(格式很糟糕),提到在这一行 for neighbor in neighbors:
中,neighbors
是未定义的。编译器绝对不可能在中间的行中顺利通过而没有其他错误。
(x, y) = self.place
neighbors = [ (x+1, y), (x-1, y)]
没有其他错误的话,它是不会继续执行的。
注意:上面的内容是对另一个问题的回应,而那个问题在我回答的时候已经被关闭了。 我把它留在这里是为了让你知道,你在那个问题上得到的建议是错误的。
我怀疑在你提到的 第一个问题 中(在 for x in range( width ):
这一行中 width
没有定义),你没有修复所有的缩进错误,而在 Q2 和 Q3 中的 for neighbor in neighbors:
这一行应该向右移动 4 个空格。
你的源文件中有没有制表符?如果有,赶紧去掉它们。 如果你不确定怎么保持文件中没有制表符,可以单独提问,说明你使用的编辑器和操作系统,或许有人能帮你。
如何在源文件中查找制表符:
C:\junk>\python27\python -c "print[x for x in enumerate(open('sometabs.py'),1)if'\t'in x[1]]"
[(1, 'foo\tbar\n'), (3, '\t\toof\n')]