Python requiring an attribute be converted to a string
from sys import exit
from random import randint
class Map(object):
def death():
print quips[randint (0, len(quips)-1)]
exit(1)
def princess_lives_here():
print "You see a beautiful Princess with a shiny crown."
print "She offers you some cake."
eat_it = raw_input(">")
if eat_it == "eat it":
print "You explode like a pinata full of frogs."
print "The Princess cackles and eats the frogs. Yum!"
return 'death'
elif eat_it == "do not eat it":
print "She throws the cake at you and it cuts off your head."
print "The last thing you see is her munching on your face. Yum!"
return 'death'
elif eat_it == "make her eat it":
print "The Princess screams as you cram the cake in her mouth."
print "Then she smiles and cries and thank you for saving her."
print "She points to a tiny door and says, 'The Koi needs cake too.'"
print "She gives you the very last bit of cake and shoves you in."
return 'gold_koi_pond'
else:
print "The Princess looks at you confused and just points at the cake."
return 'princess_lives_here'
class Engine(object):
def __init__(self, start, quips):
self.quips = [
"You died. You开发者_运维技巧 suck at this.",
"Your mom would be proud, if she were smarter",
"Such a luser.",
"I have a small puppy that's better at this."
]
self.start = start
def play(self):
next = self.start
while True:
print "\n-----"
room = getattr(self, next)
next = room()
m = Map()
e = Engine(m, "princess_lives_here")
e.play()
Trace back i get in terminal is:
Traceback (most recent call last):
File "ec42.py", line 162, in <module>
e.play()
File "ec42.py", line 156, in play
room = getattr(self, next)
TypeError: getattr(): attribute name must be string
I have been working on this far too long, and just cannot nail it down. The main issue is getting the map class to run inside the engine class as an object. thanks in advance for the help.
Maybe you want something like this?
class Map(object):
def __init__(self):
self.quips = [
"You died. You suck at this.",
"Your mom would be proud, if she were smarter",
"Such a luser.",
"I have a small puppy that's better at this."
]
def death(self):
print self.quips[randint (0, len(self.quips)-1)]
exit(1)
def princess_lives_here(self):
print "You see a beautiful Princess with a shiny crown."
print "She offers you some cake."
eat_it = raw_input(">")
if eat_it == "eat it":
print "You explode like a pinata full of frogs."
print "The Princess cackles and eats the frogs. Yum!"
return 'death'
elif eat_it == "do not eat it":
print "She throws the cake at you and it cuts off your head."
print "The last thing you see is her munching on your face. Yum!"
return 'death'
elif eat_it == "make her eat it":
print "The Princess screams as you cram the cake in her mouth."
print "Then she smiles and cries and thank you for saving her."
print "She points to a tiny door and says, 'The Koi needs cake too.'"
print "She gives you the very last bit of cake and shoves you in."
return 'gold_koi_pond'
else:
print "The Princess looks at you confused and just points at the cake."
return 'princess_lives_here'
class Engine(object):
def __init__(self, map, start):
self.quips = [
"You died. You suck at this.",
"Your mom would be proud, if she were smarter",
"Such a luser.",
"I have a small puppy that's better at this."
]
self.map = map
self.start = start
def play(self):
next = self.start
while True:
print "\n-----"
room = getattr(self.map, next)
next = room()
def __init__(self, start, quips):
...
e = Engine(m, "princess_lives_here")
This is your problem. The second line calls init with arguments m and "princess_lives_here". The first argument should be "princess_lives_here" and the second should be the "quips" list.
To return the value of a named attribute of an object you must supply a string of the name of the attribute.
room = getattr(self, 'next')
From the python docs: getattr(object, name[, default])
Return the value of the named attribute of object.name must be a string. If the string is the name of one of the object’s attributes, the result is the value of that attribute. For example, getattr(x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, default is returned if provided, otherwise AttributeError is raised.
self.start is an instance of Map, but the second argument of getattr() must be a string, the name of the attribute.
Also, instance methods should have "self" as the first argument.
The error message actually tells you quite clearly what is wrong here :-)
room = getattr(self, next)
should go more along the lines of
room = getattr(self, 'next')
as described in the Python documentation
But this is only part of the problem here. If I guess right on what you've omitted in your code sample you will probably also need to add a __call__ method to your map object. Otherwise room = getattr(self, next)
doesn't make all that much sense :-) Well that and the rest of the code that is probably missing here ;-)
Also all the method definitions on the Map class are missing the "self" argument.
精彩评论