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

2024-04-20 12:57:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我查到的终端是:

^{pr2}$

我在这方面做得太久了,就是不能把它搞定。主要问题是让map类作为一个对象在engine类中运行。提前谢谢你的帮助。在


Tags: andtheselfyoudefitatprint
3条回答

也许你想要这样的东西?在

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

若要返回对象的命名属性的值,必须提供该属性名称的字符串。在

room = getattr(self, 'next')

来自python文档: getattr(object,name[,default])

返回的命名属性的值对象.名称必须是字符串。如果字符串是对象属性之一的名称,则结果是该属性的值。例如,getattr(x,'foobar')相当于x.foobar。如果命名属性不存在,则返回default(如果提供),否则引发AttributeError。在

def __init__(self, start, quips):

。。。在

^{pr2}$

这是你的问题。第二行调用init,参数为m和“公主住在这里”。第一个论点应该是“琰公主住在这里”,第二个论点应该是“俏皮话”清单。在

相关问题 更多 >