2015-11-06 74 views
-4

我正在學習與學習Python硬盤的方式Python和我使用字典和類製作一個「遊戲」。代碼不完整,但主要問題是AttributeError。AttributeError的:「Nonetype」對象有沒有屬性「進入」蟒蛇

我堅持了這個錯誤:

Traceback (most recent call last): 
    File "juego.py", line 86, in <module> 
    juego.play() 
    File "juego.py", line 60, in play 
    game.enter() 
AttributeError: 'NoneType' object has no attribute 'enter' 

代碼:

class entry(): #OK 
    def enter(self): 
     print 'Llegaste a la primera habitacion, miras a tu alrededor...' 
     print 'y ves un boton rojo en la pared...' 
     print 'Que haces?' 
     print 'Apretas el boton?' 
     print 'O seguis mirando?' 
     boton_rojo = raw_input('> ') 
     if 'boton' in boton_rojo: 
      print 'Apretas el boton y...' 
      print 'Aparece una puerta adelante tuyo!' 
      return 'Rescate_Prisionero' 
     elif 'mir' in boton_rojo: 
      print 'Seguis mirando...' 
      print '...' 
      print 'no encontras nada y decidis apretar el boton rojo' 
      print 'Apretas el boton y...' 
      print 'Aparece una puerta adelante tuyo!' 
     else: 
      print 'eh? que dijiste?' 


class rescate_prisionero(): 
    def enter(self): 
     print 'parece que si' 
     return 'Mago_Poderoso' 

class mago_poderoso(): 
    def enter(self): 
     print 'trolo' 
     return 'Pelea_esqueleto' 

class pelea_esqueleto(): 
    def enter(self): 
     print 'esque' 
     return 'Habitacion_Vacia' 

class habitacion_vacia(): 
    def enter(self): 
     print 'vac' 
     return 'Final_Scene' 

class final_scene(): 
    def enter(self): 
     print 'parece que esta todo bien' 


class Engine(object): 
    def __init__(self, primer_escena): 
    self.primer_escena = primer_escena 

    def play(self): 
     ultima_escena = Map.Escenas.get('Final_Scene') 
     game =self.primer_escena.arranque().enter() 
     while game != ultima_escena: 
     game = Map.Escenas.get(game) 
     game.enter() 



class Map(): 
    def __init__(self, primer_escena): 
    self.primer_escena = primer_escena 

    def arranque(self): 
    inicio = Map.Escenas.get(self.primer_escena) 
    return inicio 





Escenas = { 'Entry' : Entry(), 
      'Rescate_Prisionero' : rescate_prisionero(), 
      'Mago_Poderoso' : mago_poderoso(), 
      'Pelea_esqueleto' : pelea_esqueleto(), 
      'Habitacion_Vacia' : habitacion_vacia(), 
      'Final_Scene' : final_scene() 
} 

pepe = Map('Entry') 
juego = Engine(pepe) 
juego.play() 

編輯:對不起,我忘了錯誤,代碼現在已經完成

+1

你忘了給我們的錯誤消息。 –

+1

你也忘了發佈整個代碼。 Entry沒有定義。 –

+0

我添加了錯誤,代碼是完全 – mavocado

回答

0

你得到這個錯誤,因爲在某個點game成爲None對象。這可能發生在許多不同的步驟,因此可能很難追查。你調用任何時候dict.get您返回None如果關鍵是沒有找到。

這可能比較容易所有dict.get(key)語句改爲dict[key]語句。雖然目前沒有出現任何的方式進行,如果字典中不包含的關鍵,所以扔KeyError絕對比抑制錯誤直到後來更好。

也有可能一個或多個Map.Escenas值實際上是None,因此即使dict.get找到該值,它也會返回None。這就是爲什麼在這裏使用dict[key]有幫助 - 至少你可以縮小範圍!你沒有任何的功能包括任何代碼:

Entry 
rescate_prisionero 
mago_poderoso 
pelea_esqueleto 
habitacion_vacia 
final_scene 

因此,有沒有辦法讓我們來檢查這裏。

+0

是的,我忘了補充一點。代碼現在已經完成,謝謝你的回答,我會嘗試 – mavocado

相關問題