-1

I'm working on a DND project that completely creates a character for you. I want to make it so if an ability score is a certain number or within a certain range of numbers, the program will adjust the ability score.

def __init__(self, name, race, sub_race, char_class, size='Small', speed=0, strength=0, dexterity=0, constitution=0,
                 intelligence=0, wisdom=0, charisma=0):
        self.name = name
        self.race = race
        self.sub_race = sub_race
        self.char_class = char_class
        self.size = size
        self.speed = speed
        self.strength = strength
        self.dexterity = dexterity
        self.constitution = constitution
        self.intelligence = intelligence
        self.wisdom = wisdom
        self.charisma = charisma

I'd like to make it so the abilities (speed, strength, dexterity, constitution, intelligence, wisdom, charisma) are in a dictionary within the class. This way I can update each individual ability score.

    def adjust_scores(self):
        if (self.speed or self.strength or self.dexterity or self.constitution or self.intelligence or self.wisdom or
            self.charisma) == 1:

Is there a way to make these scores into a dictionary and possibly iterate through each key/value pair and update their values?

  • 3
    `if (self.speed or self.strength or self.dexterity or self.constitution or self.intelligence or self.wisdom or self.charisma) == 1:` [does not do what you think it does](https://stackoverflow.com/q/15112125/364696). – ShadowRanger Jul 07 '22 at 01:34
  • How would a dictionary help? The posted code is fully able to adjust individual scores. – John Gordon Jul 07 '22 at 01:39
  • The `== 1` part of the condition is wrong. You need to use `x == 1 or y == 1 or z == 1`. – Tom Karzes Jul 07 '22 at 01:43
  • If you want those variables to be a dictionary, then ... make them a dictionary. What is the difficulty? – John Gordon Jul 07 '22 at 01:49
  • Does this answer your question? [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value) – Silvio Mayolo Jul 07 '22 at 03:08

1 Answers1

0

The solution is actually quite simple. You can store a dictionary instead of attributes, like you are asking in the question, as shown below:

def __init__(self, name, race, sub_race, char_class, size='Small', speed=0, strength=0, dexterity=0, constitution=0,
                 intelligence=0, wisdom=0, charisma=0):
        self.attributes = {}
        self.attributes['name'] = name
        self.attributes['race'] = race
        self.attributes['sub_race'] = sub_race
        self.attributes['char_class'] = char_class
        self.attributes['size'] = size
        self.attributes['speed'] = speed
        self.attributes['strength'] = strength
        self.attributes['dexterity'] = dexterity
        self.attributes['constitution'] = constitution
        self.attributes['intelligence'] = intelligence
        self.attributes['wisdom'] = wisdom
        self.attributes['charisma'] = charisma

If you want to retain your specific attributes as well as the dictionary, you can have the best of both worlds by having the constructor be:

def __init__(self, name, race, sub_race, char_class, size='Small', speed=0, strength=0, dexterity=0, constitution=0,
                 intelligence=0, wisdom=0, charisma=0):
        self.attributes = {}
        self.attributes['name'] = name
        self.attributes['race'] = race
        self.attributes['sub_race'] = sub_race
        self.attributes['char_class'] = char_class
        self.attributes['size'] = size
        self.attributes['speed'] = speed
        self.attributes['strength'] = strength
        self.attributes['dexterity'] = dexterity
        self.attributes['constitution'] = constitution
        self.attributes['intelligence'] = intelligence
        self.attributes['wisdom'] = wisdom
        self.attributes['charisma'] = charisma
        self.name = name
        self.race = race
        self.sub_race = sub_race
        self.char_class = char_class
        self.size = size
        self.speed = speed
        self.strength = strength
        self.dexterity = dexterity
        self.constitution = constitution
        self.intelligence = intelligence
        self.wisdom = wisdom
        self.charisma = charisma

and either:

1 - when changing one of them, change the other 2 - have a special setter function for each attribute, which also changes the dict. Ex:

def setName(self, newName):
    self.name = newName
    self.attributes['name'] = newName

Edit: Sorry for my incorrect remarks about setters previously. As pointed out in comments, setters and the second method are generally a bad practice in Python (this doesn't apply to other languages such as Java, with specifications such as private).

However, this is by no means stopping you from using that method. If I understand your use-case correctly, then you shouldn't be extremely nitpicky at conventions, etc.

Ryan Fu
  • 349
  • 1
  • 5
  • 22