Skip to content Skip to sidebar Skip to footer

Python Dictionary Not Returning New Value Like I Wanted

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'''

Solution 1:

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

Post a Comment for "Python Dictionary Not Returning New Value Like I Wanted"