gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:10:28 25 4
gpt4 key购买 nike

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 类作为对象在引擎类中运行。预先感谢您的帮助。

最佳答案

也许你想要这样的东西?

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

关于Python 要求将属性转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6208418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com