Python字典不会像我想要的那样返回新的价值 [英] Python Dictionary Not Returning New Value Like I Wanted
问题描述
类英雄():
/ pre>
让我们做一些heros shit
def __init __(self,name =Jimmy,prof =Warrior,weapon =Sword):
英雄构造器
self.name = name
self.prof = prof
self.weapon = weapon
self.herodict = {
名称:self.name,
类:self.prof,
武器:self.weapon
}
self.herotext = {
欢迎:问候,英雄,你的名字是什么?,
AskClass:一个好名字%s。你的班级是什么?%self.herodict ['Name'],
}
def thingy(self,textkey,herokey):
n = raw_input(self.herotext [textkey])
self.herodict [herokey] = n
如果self.herodict [herokey]!=:
print self.herodict [herokey]
else:
self.herodict [herokey] = self.name
print self.herodict [herokey]
def setHeroName(self):
self.thingy( Welcome,'Name')
def setHeroClass(self):
self.thingy(AskClass,'Class')
所以基本上你运行它,它设置了一堆默认值。它会询问一个名称,并给它用户输入,并将Name键设置为输入的新值。他们假设要使用新的键:value或name:(userinput),并在下一个小句子中使用它。而是返回并使用默认值jimmy。
我需要做什么?
解决方案
AskClass:一个很好的名字%s。你的班级是什么?%self.herodict ['Name'] ,
在创建课程时执行,而不是在以后打印时执行。当您创建课程时,
self.herodict ['Name']
设置为'Jimmy'
。
稍后您必须进行插值,当您确实有一个名称。也许你需要使用callable,如
lambda
对象:self .herotext = {
欢迎:lambda self:问候,英雄,你的名字是什么?,
AskClass:lambda self:一个好名字%s。你的班级是什么? %self.herodict ['Name'],
}
然后调用它们传递
self
以后:n = raw_input(self.herotext [textkey ](自))
So clearly Im doing something wrong. Im a new python/noob coder so it may be obvious for many of you but Im not sure what to do.
class hero(): """Lets do some heros shit""" 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 your class? " % self.herodict['Name'], } def thingy(self, textkey, herokey): n = raw_input(self.herotext[textkey]) self.herodict[herokey] = n if self.herodict[herokey] != "": print self.herodict[herokey] else: self.herodict[herokey] = self.name print self.herodict[herokey] def setHeroName(self): self.thingy("Welcome", 'Name') def setHeroClass(self): self.thingy("AskClass", 'Class')
So basically you run this and it sets a bunch of default values. It will ask a name, and you give it user input, and it sets the Name key to the new value of your input. They it is suppose to take that new key:value or name:(userinput) and use it in the next little sentence. But instead its going back and using the default value of 'jimmy'.
What do I need to do?
解决方案The line:
"AskClass": "A fine name %s. What is your class? " % self.herodict['Name'],
is executed when you create the class, not when you later print it. When you create the class,
self.herodict['Name']
is set to'Jimmy'
.You'll have to do the interpolation later on, when you actually have a name. Perhaps you need to use callables instead, like
lambda
objects:self.herotext = { "Welcome": lambda self: "Greetings, hero. What is thine name? ", "AskClass": lambda self: "A fine name %s. What is your class? " % self.herodict['Name'], }
then call them passing in
self
later on:n = raw_input(self.herotext[textkey](self))
这篇关于Python字典不会像我想要的那样返回新的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!