我无法弄清楚为什么我会得到AttributeError

时间:2016-07-02 18:41:53

标签: python class object attributeerror

亲爱的StackOverflow天使,

我真的很困惑这个问题。我正在编写游戏并收到此错误消息:

File "VillageGame.py", line 120, in <module>
launch.play()
File "VillageGame.py", line 84, in play
returned_scene = future_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'

以下是错误消息引用的代码:

class Room(object):

    def __init__(self):
        pass

    def enter(self):
        pass  

class Road(Room): 

    def __init__(self):
        pass

    def enter(self):
        pass  

    def walk(self): 
        print "It is afternoon and you stand by the road of Red Village. It is a small, run-down place"
        print "with a few shops and houses. A few residents walk about. It is a quiet place. You need"
        print "to steal gold to feed yourself and pay your rent. This evening you need to steal 800 gold"
        print "to pay your rent and bills. There are houses and businesses in the area. But plan carefully,"
        print "the police here are armed and the locals are suspicious of odd outsiders like yourself. But"
        print "you can fight, which should help. In front of you is a food shop, a weapons shop,"
        print "a medicine shop, a bank and an inn. Where would you like to go first?" 
        return 'road' 

    def run(self): 
        pass        



class Engine(object):

    def __init__(self, game_map):
        self.game_map = game_map

    def play(self):

        future_scene = self.game_map.launch_scene()
        last_scene = self.game_map.next_scene('finished') 

        while future_scene != last_scene:
            returned_scene = future_scene.enter() 
            future_scene = self.game_map.next_scene(returned_scene)

        # make sure you run the last scene
        future_scene.enter()



class Map(object):

    rooms = {
        'road' : Road(),
        'food_shop' : FoodShop(),
        'weapons_shop' : WeaponsShop(),
        'medicine_shop' : MedicineShop(),
        'bank' : Bank(),
        'inn' : Inn(),  
        'death' : Death(),
        'finished' : Finished() 
        }        


    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, returned_scene):
        val = Map.rooms.get(returned_scene)
        return val

    def launch_scene(self):
        return self.next_scene(self.start_scene) 



firstscene = Map('road') 
launch = Engine(firstscene) 
launch.play() 

道歉,如果这看起来像是代码转储,但我不知道哪些部分与错误消息相关,哪些部分没有链接到它 - 因为我对此很新。

如果有人对我可以删除此消息的代码有任何了解,我也会感激。这将有助于我将未来的问题缩短。

2 个答案:

答案 0 :(得分:2)

问题是class MyClass(object): class_var = 2 def __init__(self, i_var): self.instance_var = i_var setattr(MyClass, 'a', 'value-of-a-field') foo = MyClass(5) bar = MyClass(6) print(foo.class_var, foo.instance_var, foo.a) # 2 5 value-of-a-field print(bar.class_var, bar.instance_var, bar.a) # 2 6 value-of-a-field print(MyClass.a) # value-of-a-field print(MyClass.class_var) # 2 返回Map('road')。我认为你要做的就是这个。

None

但即使这样也不太对。下面的行不会实例化地图。

map = Map('road') 
firstscene = map.launch_scene()
launch = Engine(firstscene) 
launch.play()

如果您想在该课程中调用val = Map.rooms.get(returned_scene) 课程的某个功能,请使用Mapself关键字。

答案 1 :(得分:1)

这一行if isPartialResult == true { guard let _ = calculated else { log += (" \(accumulator) \(symbol)") } log += (" \(symbol)") calculated = nil } else { log += log.characters.count <= 1 ? (" \(accumulator) \(symbol)") : (" \(symbol)") } 需要一个返回值,但您尚未在方法returned_scene = future_scene.enter()中实现任何内容(您只是def enter(self) - 它,所以它返回pass个对象。