我有编译器错误“未定义”虽然有一个定义 [英] I have a compiler error "not defined" although there is a definition

查看:181
本文介绍了我有编译器错误“未定义”虽然有一个定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  from gasp import * 
GRID_SIZE = 30
MARGIN = GRID_SIZE

BACKGROUND_COLOR = color.BLACK#我们使用的颜色
WALL_COLOR =(0.6 * 255,0.9 * 255,0.9 * 255)

#迷宫的形状。每个字符
#表示不同类型的对象
#% - Wall
#。 - 食物
#o - 胶囊
#G - Ghost
#P - Chomp
#其他字符被忽略


the_layout = [
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%.....% .................%.....%,
%o %%%。%。%%%。%%%%%%% 。%%%。%。%%% o%,
%。%.....%......%......%.....%。% ,
%... %%%。%。%%%%。%。%%%%。%。%%% ...%,
%%%。% ...%。%.........%。%...%。%%%,
%...%。%%%。%。%%% %% %。%。%%%。%...%,
%。%%% .......%GG GG%....... %%%。%
%...%。%%%。%。%%%%%%。%。%%%。%...%,
%%%。 。%。%.........%。%...%。%%%,
%... %%%。%。%%%%。%。%% %%。%。%%% ...%,
%。%.....%......%......%.....%。% ,
%o %%%。%。%%%。%%%%%%%%%%%%%%%%%%,
%.... 。%........ P ........%.....%,
%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%]


class不可移动:
def is_a_wall(self):
return False

class Nothing(Immovable):
pass

class迷宫:
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(布局)
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 range in width(width):
new_row = []
for x in range(宽度):
new_row.append(Nothing())
self.map.append(new_row)

def make_object(self,point,charactor):
x,y)= point
如果charactor ==%:
self.map [y] [x] = Wall(self,point)

def finished )
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或y> = self.height:
return Nothing()
如果x < 0或x> = self.width:
return Nothing()
return self.map [y] [x]




class Wall(Immovable):
def __init __(self,maze,point):
self.place = point#存储我们的位置
self.screen_point = maze.to_screen(point)
self.maze = 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 =邻居的邻居的$($)
self.check_neighbor(neighbor)

def check_neighbor(self,邻居):
迷宫= self.maze
对象= maze.object_at(邻居)

如果object.is_a_wall():
这里= self.screen_point
there = maze.to_screen(neighbor)
行(这里,color = WALL_COLOR,thickness = 2)

def is_a_wall(self):
return True

the_maze =迷宫()

而不是__maze.finished():
the_maze.play()
the_maze.done ()

我收到这个错误..

 追溯(最近的最后一次呼叫):
文件chomp.py,第110行,在类Wall(Immovable)中:
文件chomp.py ,第124行,在邻居邻居的墙上:
名称错误:未定义名称邻居

我花了很多时间仍然找不到什么问题,需要一些帮助

解决方案

未完成的调用 Circle()可能是尝试正确格式化代码时出错。请检查您是否发布了您实际运行的代码,并且追溯是您实际获得的代码(有一些失踪!)。



报告的错误在(可怕格式)的追溯中,邻居在邻居中的邻居行中未定义:。编译器绝对没有办法通过干预的方式来解决这个问题。

 (x,y)= self.place 
neighbors = [(x + 1,y),(x-1,y)]

没有其他类型的错误。



注意:上面是针对另外一个我正在回答的问题。留下它,以便您知道您对该问题的建议是错误的。



我怀疑在您的第一个问题 width 未在行 for x in range(width):),您没有修复所有的缩进错误,邻居中的邻居的Q2和Q3行 code>应该在其显示的右侧移动4个空格。



您的源文件中有任何选项卡吗?如果是这样,请删除它们。如果您不确定如何保持标签免费,请单独提出问题,说出什么编辑器和什么操作系统以及熟悉该组合的人可以帮助您。 >

如何在源文件中查找标签:

  C:\junk> \python27\python -cprint [x for x in enumerate(open('sometabs.py')),1)如果'\\'在x [1]]
[(1,'foo\tbar\\\
'),(3,'\t\toof\\\
')]


  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()

I got this error..

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

I spent lot of time still can't find what's wrong, need some help

解决方案

The unfinished call to Circle() is probably the result of an error trying to format the code properly. Please check that you post the code that you actually ran, and the traceback is the one that you actually got (there's some of this one missing!).

The error reported in the (horribly formatted) traceback is that neighbors is undefined in the line for neighbor in neighbors:. There is absolutely no way the compiler would munch its way through the intervening lines

(x, y) = self.place
neighbors = [ (x+1, y), (x-1, y)]

without some other kind of error.

Note: the above was in response to another question that was closed as I was answering it. I am leaving it in so that you know that the advice that you got for that question was wrong.

I suspect that after your FIRST question (width not defined in line for x in range( width ):), you didn't fix all your indentation errors, and the Q2 and Q3 line for neighbor in neighbors: should be shifted 4 spaces to the right of where it appears to be.

Do you have any tabs in your source files? If so, get rid of them. If you are not sure about how to stay tab-free, ask a separate question, say what editor and what OS and maybe someone familiar with that combination can help you.

How to find tabs in your source file:

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')]

这篇关于我有编译器错误“未定义”虽然有一个定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆