【问题标题】:I cannot figure out why I am getting AttributeError我无法弄清楚为什么我会收到 AttributeError
【发布时间】:2016-11-04 22:13:40
【问题描述】:

亲爱的 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() 

如果这看起来像是代码转储,我深表歉意,但我不知道哪些部分与错误消息相关以及哪些部分与错误消息无关 - 因为我对此很陌生。

如果有人对我可以从这条消息中删除哪些代码有任何见解,我也将不胜感激。这将帮助我缩短未来的问题。

【问题讨论】:

    标签: python class object attributeerror


    【解决方案1】:

    这个行returned_scene = future_scene.enter()预期返回值,但你没有在方法def enter(self)(你只是pass -ing它,所以它返回一个None对象。 p >

    【讨论】:

    • 嗨,我在'def walk(self)'中移动到'def enter(self)'中的所有代码,我收到此错误消息:File "VillageGame.py", line 118, in &lt;module&gt; launch.play() File "VillageGame.py", line 77, in play future_scene = self.game_map.launch_scene() AttributeError: 'Road' object has no attribute 'launch_scene' span>
    【解决方案2】:

    问题是Map('road') 返回None。我想你想要做的是这个。

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

    但即使这样也不完全正确。下面的行没有实例化地图。

    val = Map.rooms.get(returned_scene)
    

    如果您想从该类中调用Map 类的函数,请使用self 关键字,例如self.rooms.get(returned_scene)

    【讨论】:

    • 我尝试了所有的编辑 - 将 val 行更改为:val = self.rooms.get(returned_scene) 并将此:firstscene = Map('road') launch = Engine(firstscene) launch.play() 更改为:map = Map('road') firstscene = map.launch_scene() launch = Engine(firstscene) launch.play() 我刚刚收到此错误消息:File "VillageGame.py", line 121, in &lt;module&gt; launch.play() File "VillageGame.py", line 80, in play future_scene = self.game_map.launch_scene() AttributeError: 'Road' object has no attribute 'launch_scene'
    • self.game_map 指的是引擎中的 Road() 对象,并且您在 Road() 上调用 launch_scene()。 Road 没有那种方法,所以解释器告诉你。我不是 100% 确定你在这里的意图是什么,但我认为从 future_scene = self.game_map.launch_scene() 行中删除 .launch_scence() 将朝着正确的方向发展。让我知道情况如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    相关资源
    最近更新 更多