2016-07-02 8 views
2

Liebe StackOverflow Angels,Ich kann nicht herausfinden, warum ich AttributeError

Ich bin wirklich mit diesem Problem fest. Ich entwickle ein Spiel und erhalte diese Fehlermeldung:

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' 

Hier ist der Code, der von der Fehlermeldung verwiesen wird:

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

Entschuldigt, wenn dies wie ein Code-Dump scheint, aber ich weiß nicht wissen, welche Teile für die Fehlermeldung relevant sind und welche Teile nicht damit verbunden sind - weil ich ziemlich neu darin bin, sehen Sie.

Wenn jemand einen Einblick hat, welchen Code ich aus dieser Nachricht herausschneiden könnte, würde ich das auch schätzen. Es würde mir helfen, zukünftige Fragen kürzer zu halten.

Antwort

1

Diese Linie returned_scene = future_scene.enter() erwartet einen Rückgabewert, aber sie haben nichts in dem Verfahren implementiert def enter(self) (du bist nur pass -ing, und es gibt so ein None Objekt

+0

Hallo, ich habe den ganzen Code in der 'def gehen (Selbst)' auf 'def eingeben (Selbst)' und ich bekomme diese Fehlermeldung: 'Datei" VillageGame.py ", Zeile 118, in starten. play() Datei "VillageGame.py", Zeile 77, im Spiel future_scene = self.game_map.launch_scene() AttributeError: 'Road' Objekt hat kein Attribut 'launch_scene'' –

+0

Problem gelöst. Vielen Dank! –

2

Das Problem ist Map('road') gibt None zurück. Ich denke, was du versuchst zu tun, ist dies.

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

Aber auch das ist nicht ganz richtig. Die folgende Zeile instanziiert keine Karte.

val = Map.rooms.get(returned_scene) 

Wenn Sie innerhalb dieser Klasse eine Funktion der Map Klasse aufrufen möchten, verwenden Sie das Schlüsselwort self wie self.rooms.get(returned_scene).

+0

ich alle Ihre Bearbeitungen versucht. - Ändern val Linie: 'val = self.rooms.get (returned_scene)' und änderte sich dies: 'firstscene = Karte ('Straße') Start = Engine (firstscene) launch.play() ' zu: 'map = Karte ('Straße') firstscene = map.launch_scene() Start = Engine (firstscene) launch.play()' ich diese Fehlermeldung nur bekam: 'File„VillageGame.py “, Linie 121, in launch.play() File "VillageGame.py", Zeile 80, im Spiel future_scene = self.game_map.launch_scene() Attribute: 'Straße' Objekt hat kein Attribut‚launch_scene'' –

+0

'self.game_map' bezieht sich auf ein' Road() 'Objekt innerhalb Ihrer Engine und Sie rufen' launch_scene() 'auf einem' Road() 'auf. Road hat diese Methode nicht, also sagt Ihnen der Dolmetscher das. Ich bin mir nicht 100% sicher, was Sie hier vorhaben, aber ich denke, das Entfernen von '.launch_scence()' aus der Zeile 'future_scene = self.game_map.launch_scene()' wird sich in die richtige Richtung bewegen. Lass mich wissen, wie das geht. – user2027202827

Verwandte Themen