python代码,需要帮助。名称未定义错误
我得到了这个错误:python代码,需要帮助。名称未定义错误,python,Python,我得到了这个错误: from gasp import * GRID_SIZE = 30 MARGIN = GRID_SIZE BACKGROUND_COLOR = (0,0,0) # 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 # % - Wa
from gasp import *
GRID_SIZE = 30
MARGIN = GRID_SIZE
BACKGROUND_COLOR = (0,0,0) # 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:
pass
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 = []
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, # Just draw circle
color = WALL_COLOR, filled=True)
the_maze = Maze()
while not the_maze.finished():
the_maze.play()
the_maze.done()
Andy@Macbook-Pro~/Documents/workspace/pythoncode$python gasp.py
回溯(最近一次呼叫最后一次):
文件“gasp.py”,第1行,在
从gasp进口*
文件“/Users/Andy/Documents/workspace/pythoncode/gasp.py”,第42行,在
班级迷宫:
文件“/Users/Andy/Documents/workspace/pythoncode/gasp.py”,第55行,在迷宫中
对于范围内的x(宽度):
NameError:未定义名称“宽度”
您可能在范围(宽度)中x的行前面有缩进问题:
导致Python认为该行与def
行处于同一级别。请清除源文件中的制表符,然后重试。您可能在范围(宽度)中x的行前面有缩进问题:
导致Python认为该行与def
行处于同一级别。请清除源文件中的制表符,然后重试。第55行的宽度两侧是否有不可打印/非空白字符?第55行的宽度两侧是否有不可打印/非空白字符?为什么模块名为gasp.py,但您的开头语句是“from gasp import*”?为什么模块名为gasp.py,而您的开场白是“from gasp import*”?尝试更改文件名
从gasp进口
它们是相同的尝试更改文件名 从gasp进口
它们是相同的,会导致引用特定字符的语法错误
width
看起来已定义,但它没有定义--这是一个空白问题。这将导致引用特定字符时出现语法错误<代码>宽度看起来已定义,但它没有定义--这是一个空白问题。原因可能与y和x都在迷宫中迭代范围(宽度)相同。make_-map方法。原因可能与y和x都在迷宫中迭代范围(宽度)相同。make_-map方法。无法重现名称错误
(在注释掉所有缺失的函数和导入之后)。如果有问题的行55被删除了4个空格,则可以复制它(无论导入是否完成)。无法复制NameError
(注释掉所有缺失的函数和导入之后)。如果有问题的行55被4个空格删除,则可以复制它(无论导入是否完成)。
Andy@Macbook-Pro~/Documents/workspace/pythoncode$python gasp.py
Traceback (most recent call last):
File "gasp.py", line 1, in <module>
from gasp import *
File "/Users/Andy/Documents/workspace/pythoncode/gasp.py", line 42, in <module>
class Maze:
File "/Users/Andy/Documents/workspace/pythoncode/gasp.py", line 55, in Maze
for x in range( width ):
NameError: name 'width' is not defined