0
class hero():

    def __init__(self, name="Jimmy", prof="Warrior", weapon="Sword"):
        """Constructor for hero"""
        self.name = name
        self.prof = prof
        self.weapon = weapon
        self.herodict = {
            "Name": self.name,
            "Class": self.prof,
            "Weapon": self.weapon
        }
        self.herotext = {
            "Welcome": "Greetings, hero. What is thine name? ",
            "AskClass": "A fine name %s. What is thine class? " % self.herodict['Name'],
            "AskWeapon": "A %s ? What shalt thy weapon be? " % self.herodict['Class'],
        }

    def setHeroDict(self, textkey, herokey):
        n = raw_input(self.herotext[textkey])
        self.herodict[herokey] = n
        print self.herodict[herokey]



h = hero("Tommy", "Mage", "Staff")
h.setHeroDict("Welcome", "Name")
h.setHeroDict("AskClass", "Class")

好吧,我之前在这里问过一次,一个聪明的人告诉我尝试使用 lambdas。我试过了,它奏效了。伟大的!但是我的问题有点不同。正如我在那里所说,我对此很陌生,并且我的知识中有很多我试图填补的漏洞。基本上..我如何在不使用 lambdas 的情况下做得更好(或者人们通常为此使用 lambdas 吗?)

我想做什么:

  1. 有一个带有一些变量的英雄类,这些变量附加了一些默认值。
  2. 然后,我想使用一个定义,该定义可以使用 myherotext来使用其中一个值来提出问题。
  3. 用户然后回答问题,然后防御继续并更改适当的值herodict

我试图通过的问题: 在我的herotext我有一个值,它本身指向一个键herodict。如链接中所述,我了解到这是由于在用户可以提供输入之前herodict被初始化为默认值。herotext因此它会打印出默认的(在本例中为 Tommy)名称,而不是“AskClass”self.herodict['Name']值中的新用户输入名称。

我该如何解决?我不介意我是否必须制作另一个文件或其他什么,我只想知道做这种事情的更合乎逻辑的方式是什么?我整天都被困在这上面,我的想法是朋友。我知道这对你们很多人来说可能很简单,我希望你能分享你的知识。

谢谢

4

2 回答 2

1

干得好。这是一种非常干净的方法。很快,我将发布我的课程版本。:-)(好吧,我本来打算这样做,但这已经很冗长了..)

class hero():
    def __init__(self, name="Jimmy", prof="Warrior", weapon="Sword"):
        """Constructor for hero"""
        self.name = name
        self.prof = prof
        self.weapon = weapon
        self.herodict = {
            "Name": self.name,
            "Class": self.prof,
            "Weapon": self.weapon
        }
        self.herotext = {
            "Welcome": "Greetings, hero. What is thine name? ",
            "AskClass": "A fine name {Name}. What is thine class? ",
            "AskWeapon": "A {Class}? What shalt thy weapon be? ",
        }

    def setHeroDict(self, textkey, herokey):
        n = raw_input(self.herotext[textkey].format(**self.herodict))
        self.herodict[herokey] = n
        print self.herodict[herokey]


h = hero("Tommy", "Mage", "Staff")
h.setHeroDict("Welcome", "Name")
h.setHeroDict("AskClass", "Class")

解释:

'format' 只是一个关于 % 所做的更新的东西。上面的行也可以使用 % 方法。这两个是等价的:

"Hello, {foo}".format(**{'foo': 'bar'})
"Hello, %(foo)s!" % {'foo': 'bar'}

无论哪种方式,我们的想法都是避免覆盖您的模板字符串。在您创建字符串模板时,您正在使用它们,然后将值分配给变量。

就像 5 * 10 总是被 50 替换一样,'meow%s' % 'meow!' 总是替换为“喵喵!”。五、十和两种喵喵声都会自动被垃圾收集,除非在别处有对它们的引用。

>>> print 5 * 10
50
>>> # the five, ten, and the 50 are now gone.
>>> template = "meow {}"
>>> template.format('splat!')
'meow splat!'
>>> # 'splat!' and 'meow splat!' are both gone, but your template still exists.
>>> template
'meow {}'
>>> template = template % 'hiss!'  # this evaluates to "template = 'meow hiss!'"
>>> template  # our template is now gone, replaced with 'meow hiss!' 
'meow hiss!'

..so,将您的模板存储在一个变量中,并且不要使用您使用它们创建的字符串“保存”它们,除非您已完成模板并且这样做是有意义的。

于 2013-06-01T02:02:38.880 回答
1

你需要使用字典吗?我认为如果您使用简单的类变量,它可能会更直接。

class Hero:
def __init__(self, name = "Jimmy", prof = "Warrior", weapon="Sword"):
    self.name = name
    self.prof = prof
    self.weapon = weapon

然后使用单独的函数向用户询问信息。

def create_hero():
    name = input("Greetings, hero. What is thine name? ")
    prof = input("A fine name %s. What is thine class?" % name)
    weapon = input("A %s ? What shalt thy weapon be?" % prof)
    return hero(name, prof, weapon)

运行它h = create_hero()

字典通常用于与关联列表(即一组对)具有相同样式的数据。

于 2013-06-01T02:09:47.733 回答