【问题标题】:AttributeError: 'Nonetype' object has no attribute 'enter' pythonAttributeError:“Nonetype”对象没有属性“输入”python
【发布时间】:2015-11-06 17:19:06
【问题描述】:

我正在通过艰难的方式学习 python,我正在使用 dict 和类制作一个“游戏”。代码不完整,但主要问题是AttributeError。

我遇到了这个错误:

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

编辑:对不起,我忘记了错误,代码现在完成了

【问题讨论】:

  • 你忘记给我们错误信息了。
  • 你也忘了发布整个代码。 Entry 未定义。
  • 我添加错误,代码完整

标签: python oop


【解决方案1】:

您收到此错误是因为在某些时候 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

所以我们没有办法在这里检查。

【讨论】:

  • 是的,我忘了补充。代码已经完成,谢谢你的回答,我去试试
猜你喜欢
  • 1970-01-01
  • 2017-12-28
  • 2017-10-05
  • 2018-03-17
  • 2019-01-10
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多