0

Im making a D&D related program because i like doing character sheets by myself and im making it simpler but i've run into a problem

def calculateMod(x):
    for n in range (0):
        base= x -10
        base2=base/2
        nMod = math.floor(base2)

With the xMod variable i'm trying to take the Ability Score that they want to calculate the modifier for (if you don't understand exactly what i'm talking about its ok its not python related) however i've created the character into a class, so the strength for example is more of a variable less of a string, so if i want to calculate the modifier for strength i would put

calculateMod(character.strength)

so for xMod i would like it to be strengthMod so i can differentiate from my other modifiers. Is there any way to do this?

DamienJ
  • 3
  • 3
  • What is `range (0)` supposed to do? How many times do you expect that loop to execute? – ChrisGPT was on strike Jul 06 '22 at 18:52
  • There is no `xMod` variable. Do you mean `x` or `nMod`? – jarmod Jul 06 '22 at 18:53
  • It would make sense for your class to have one dict for stats, and another for modifiers; so whenever your stats change, you can just iterate over the stat dict and update all the modifiers in the modifier dict. Because it's just two dictionaries, you don't need a separate variable for each stat; instead, it's just the dictionary items that scale that way. – Charles Duffy Jul 06 '22 at 23:25

1 Answers1

0

Use a dictionary to track all the characters mods, such as:

character.statmods = {
    "str": 3,
    "dex": 1,
    "con": 2,
    etc
}

if you really want them to each be a separate attribute on the character object, you can use setattr(object, attribute, value) to dynamically set attributes of an object, as long as the class of object is not restricted to using slots only.

stat = "strength"
setattr(character, stat+"Mod", nMod)

character.strengthMod #== nMod
nigh_anxiety
  • 1,428
  • 2
  • 4
  • 12