I'm learning python with learn python the hard way and I'm making a 'game' using dict and classes. The code is incomplete, but the main problem is the AttributeError.
I'm stuck with this error:
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'
The code:
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()
EDIT: sorry, I forgot the error, the code is NOW complete
You're getting this error because at some point game
becomes the None
object. This can happen at a number of different steps, so it might be hard to track down. Any time you call dict.get
you are returning None
if the key isn't found.
It's probably easiest to change all your dict.get(key)
statements to dict[key]
statements. There doesn't appear to be any way to proceed if the dictionary doesn't contain the key, so throwing a KeyError
is definitely better than suppressing the error until later.
It's also possible that one or more of your Map.Escenas
values are actually None
, so even if dict.get
is finding the value, it's returning a None
. This is why using dict[key]
is helpful here -- at least you can narrow it down! You didn't include any of the code from any of the functions:
Entry
rescate_prisionero
mago_poderoso
pelea_esqueleto
habitacion_vacia
final_scene
So there's no way for us to check that here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.