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()
跟踪我进入终端是:
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
我一直在研究这个问题太久了,并且无法确定它。主要问题是使map类在引擎类中作为对象运行。提前感谢您的帮助。
答案 0 :(得分:1)
也许你想要这样的东西?
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()
答案 1 :(得分:1)
def __init__(self, start, quips):
...
e = Engine(m, "princess_lives_here")
这是你的问题。第二行使用参数m和“princess_lives_here”调用 init 。第一个参数应该是“princess_lives_here”,第二个参数应该是“quips”列表。
答案 2 :(得分:0)
要返回对象的命名属性的值,您必须提供属性名称的字符串。
room = getattr(self, 'next')
来自python文档: getattr(对象,名称[,默认])
返回object.name 的命名属性的值必须是字符串。如果字符串是对象属性之一的名称,则结果是该属性的值。例如,getattr(x,&#39; foobar&#39;)等同于x.foobar。如果named属性不存在,则返回default(如果提供),否则引发AttributeError。
答案 3 :(得分:0)
self.start是Map的一个实例,但getattr()的第二个参数必须是一个字符串,即属性的名称。
此外,实例方法应该将“self”作为第一个参数。
答案 4 :(得分:0)
错误信息实际上告诉你这里有什么问题: - )
room = getattr(self, next)
应该更加符合
room = getattr(self, 'next')
中所述
但这只是问题的一部分。如果我想你在代码示例中省略了什么,你可能还需要在地图对象中添加__call__方法。否则room = getattr(self, next)
没有那么多意义:-)嗯,那可能在这里缺少的其余代码; - )
Map类的所有方法定义都缺少“self”参数。