2016-07-02 14 views
2

親愛なるStackOverflow天使、なぜ私はAttributeErrorを取得しているのかわかりません

私は本当にこの問題に固執しています。これはコードダンプのように思えるが、私がいない場合

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

謝罪:私はゲームをコーディングしていますし、このエラーメッセージ受信しています:ここで

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' 

すると、エラーメッセージが参照するコードですどの部分がエラーメッセージに関連しているのか、どの部分がリンクされていないのかを知ることができます。

誰かが私がこのメッセージから切り抜けることができるコードについての洞察を持っているなら、私はそれも感謝します。将来の質問をもっと短くするのに役立ちます。

答えて

1

この行returned_scene = future_scene.enter()は、戻り値を期待していますが、あなただけpassそれを-ingしている(法def enter(self)には何も実装していない、ので、それはNoneオブジェクトを返します

+0

こんにちは、私は 'def walk(self)'のすべてのコードを 'def enter(self)'に移動し、このエラーメッセージを受け取ります: 'File" VillageGame.py "、118行目、 launch。 play() ファイル "VillageGame.py"、行77、プレイ中 future_scene = self.game_map。launch_scene() AttributeError: 'Road'オブジェクトに属性がありません 'launch_scene'' –

+0

問題が解決しました。ありがとう! –

2

問題はMap('road')です。Noneです。私はあなたがしようとしていることはこれだと思います。

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

でも、これは正しくはありません。下の行はマップをインスタンス化しません。

val = Map.rooms.get(returned_scene) 

あなたは、このようなself.rooms.get(returned_scene)としてselfキーワードを使用し、そのクラス内からMapクラスの関数を呼び出す場合。

+0

私はあなたの編集のすべてを試してみました。 - valの行を変更: 'ヴァル= self.rooms.get(returned_scene)' と変更この: 'firstscene =地図( '道') 打ち上げ=エンジン(firstscene) launch.play() ' to:私は、このエラーメッセージが表示されました 'マップ=地図( '道') firstscene = map.launch_scene() 打ち上げ=エンジン(firstscene) launch.play()': 'ファイル「VillageGame.py 」、行121、 launch.play() ファイルで "VillageGame.py"、ライン80、劇中 future_scene = self.game_map.launch_scene() はAttributeError: '道' オブジェクトが「何の属性を持っていないlaunch_scene'' –

+0

'self.game_map'はエンジン内の' Road() 'オブジェクトを参照し、' Road() 'で' launch_scene() 'を呼び出しています。道路にはその方法がありませんので、通訳があなたにそれを伝えています。私はあなたがここで意図していることを100%確信していませんが、 'future_scene = self.game_map.launch_scene()'の行から '.launch_scence()'を削除するのが正しい方向に動いていると思います。私はそれがどうなるか教えてください。 – user2027202827

関連する問題