0

I have a dictionary for a text-RPG I'm working on in Python 3.9. I want to know if the list, 'atk', embedded in the dictionary can grow as the player obtains new weapons, or perhaps when they level up.

'atk', in my code, refers to the lowest and highest possible value of damage that can be inflicted on the opponent.

Mostly, I am concerned with adding a value to a list [5,13], a value such as 1, that will update my list to [6,14]. Note that "atk += 1" does not work, nor does "atk += [1,1]". Sorry if this has been covered elsewhere. If so, please link me.

char = {'name' : 'Hero',
    'lv': 3,
    'xp': 0,
    'lvnext': 83,
    'gp' : 5,
    'bag' : ["Bronze Spear"],
    'stats' : {'ATT': 1,
                'DEF': 1,
                'WIS': 1,
                'hp' : 100,
                'atk' : [5,13]}}
Simon Kissane
  • 4,373
  • 3
  • 34
  • 59
Joseph_C
  • 59
  • 4

1 Answers1

1

You'll have to be explicit.

char['stats']['atk'][0] += 1
char['stats']['atk'][1] += 1

Standard Python does not have a container that supports element-wise operations.

If you don't mind recreating the list instead of updating the existing list, you can use a list comprehension.

char['stats']['atk'] = [x + 1 for x in char['stats']['atk']]
chepner
  • 497,756
  • 71
  • 530
  • 681