Python要求将属性转换为字符串

1 投票
5 回答
2326 浏览
提问于 2025-04-16 18:48
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

我已经在这个问题上花了太多时间,始终无法解决。主要的问题是如何让地图类作为一个对象在引擎类里面运行。提前感谢大家的帮助。

5 个回答

0

要获取一个对象中某个属性的值,你需要提供这个属性的名字,名字要用字符串表示。

room = getattr(self, 'next')

根据Python的文档:

getattr(object, name[, default])

这个函数会返回对象中指定名字的属性的值。这里的名字必须是一个字符串。如果这个字符串是对象某个属性的名字,那么返回的就是这个属性的值。比如,getattr(x, 'foobar') 就等于 x.foobar。如果这个属性不存在,如果你提供了默认值,就返回默认值;如果没有提供,程序会报错,提示这个属性不存在。

1
def __init__(self, start, quips):

...

e = Engine(m, "princess_lives_here")

这是你的问题。第二行代码调用了init,传入了参数m和"princess_lives_here"。其实第一个参数应该是"princess_lives_here",而第二个参数应该是"quips"这个列表。

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

撰写回答