【问题标题】:Python - NameError: name '' is not definedPython - NameError:名称''未定义
【发布时间】:2015-10-23 20:20:10
【问题描述】:

我目前正在通过以文本格式编写程序生成的地牢关卡来扩展 Python 技能。我对为什么我的“相交”定义不起作用感到困惑。这是包含 def 的类:

class Room:

    global x1
    global x2
    global y1
    global y2

    global w
    global h

    global centre

    def __init__(self,x,y,w,h):
        x1 = x
        x2 = x + w
        y1 = y
        y2 = y + h
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        centre = math.floor((x1 + x2) / 2),math.floor((y1 + y2) / 2)

    #function that checks if the rooms intersect by comparing corner pins relative to the x,y tile map 
    def intersects(self,room):
        if x1 <= room.x2 and x2 >= room.x1 and y1 <= room.y2 and room.y2 >=  room.y1:
            return True
        return False

这里是它的名字:

def placeRooms(r):
    rooms = []
    #Where the room data is stored

    for r in range(0,r):
        w = minRoomSize + randint(minRoomSize,maxRoomSize)
        h = minRoomSize + randint(minRoomSize,maxRoomSize)
        x = randint(1,map_width - w - 1) + 1
        y = randint(1,map_height - h - 1) + 1

        newRoom = Room(x,y,w,h)

        failed = False

        #for every room generated, this function checks if new room intersects with the last one
        for otherRoom in rooms:
            if newRoom.intersects(otherRoom):
                failed = True
                break

        if failed == False:
            createRoom(newRoom)

            rooms.append(newRoom)

完整的追溯:

Traceback (most recent call last):
File "C:\Users\Max\Desktop\LiClipse Workspace\testing\RandomDungeon.py",      line 78, in <module>
placeRooms(2)
File "C:\Users\Max\Desktop\LiClipse Workspace\testing\RandomDungeon.py",  line  65, in placeRooms
if newRoom.intersects(otherRoom):
File "C:\Users\Max\Desktop\LiClipse Workspace\testing\RandomDungeon.py",   line 41, in intersects
if x1 <= room.x2 and x2 >= room.x1 and y1 <= room.y2 and room.y2 >= room.y1:
NameError: name 'x1' is not defined

我希望有人能帮助我理解为什么这段代码不起作用,谢谢。

我已经设法解决了这个问题。如果我的问题没有很好地定义,我很抱歉。我只学习了大约 4 周的 Python,而且我已经习惯了 Java,它的语法非常不同。这是我的解决方案:

def __init__(self,x,y,w,h):
    self.x1 = x
    self.x2 = x + w
    self.y1 = y
    self.y2 = y + h
    self.x = x
    self.y = y
    self.w = w
    self.h = h

【问题讨论】:

  • 您能否在您的帖子中包含完整的追溯信息?
  • 想要(或期望)你所有的global定义做什么?
  • 您应该将关键字global 的任何实例视为代码中的错误。
  • 你一直用那个global keyword,我不认为它意味着你认为它的意思。
  • 我包含了完整的回溯。 @MorganThrapp

标签: python class undefined nameerror function


【解决方案1】:

正如大多数以前的 cmets 所说,您使用的全局变量根本不应该是全局的。

按照我理解您的代码的方式,您的意思是 x1x2y1y2 是您的 Room 实例的属性,这意味着每个房间都有自己的 x1 值、x2y1y2。在 Python 中,您不必在类的开头(声明所有全局变量)声明属性,只需在 __init__ 方法中初始化属性即可。

这意味着您可以安全地删除所有global 行,并将您的__init__ 更改为

def __init__(self,x,y,w,h):
    self.x1 = x
    self.x2 = x + w
    self.y1 = y
    self.y2 = y + h
    self.w = w
    self.h = h
    centre = (self.x1 + self.x2) // 2,(self.y1 + self.y2) // 2

(请注意,您不需要math.floor,因为您已经在处理整数,只需使用整数除法运算符//

这样你定义x1y1x2y2whcenter作为你的类的属性意味着每个实例对这些变量都有自己的值.在 Python 中,您需要在所有对对象本身属性的调用之前添加self.,因此您还应该修改intersects,在每次访问当前对象的属性之前添加self.(所有x1x2 等在您的代码中尚未以 room. 为前缀)。

此外,虽然我们正在这样做,但我认为您的 intersect 函数无法按预期工作,但这是另一个问题 :)

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 2020-09-04
    • 2021-10-18
    • 2016-08-09
    • 2016-01-08
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多