0

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?

Paul Duncan
  • 302
  • 1
  • 5
  • 19

1 Answers1

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))
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I get what you mean about me having created the class not when printing. But then I looked and have a question: right after `n = raw_input(self.herotext[textkey]) self.herodict[herokey] = n` lines I have one that says `print self.herodict[herokey]` and it prints the correct new user input name (not 'Jimmy'). Is this due to it being in the same def? It seems as if they are then accessing two different dicts; one recent, and one from when the object was first initialized. – Paul Duncan May 30 '13 at 23:27
  • The `herotext` dictionary was created in the `__init__`, so when the instances is being created. The `n` local variable, however, is set when the `thingy` method runs. In that same method you then set `self.herodict[herokey]` to `n`, so when you then print `self.herodict[herokey]` it'll still have the value of `n`, which is what the user just entered when you asked for `raw_input()` input. – Martijn Pieters May 30 '13 at 23:30
  • I think Im understanding better now. Trying to imaghine it visually in my head. `Class inits->default name set->setHeroName()->thingy(newname)->LOCAL name is set->LOCAL name is printed->setHeroClass() then having already been init at the start sees the original dict default because it was made before the change` ? I suppose I should start using files to store these things in to be read back maybe instead. Or get into lambda, as Ive never used those before. Thanks for your patience and input i appreciate it! – Paul Duncan May 30 '13 at 23:39