2015-11-06 129 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

相关问题