0
attributes = {"Health": 0, "Strength": 0, "Dexterity": 0,   
"Intelligence": 0}

points = 40

def add_points(attributes):
    type = input("Which attribute would you like to adjust?: ")
    if type in attributes:
        how_many = int(input("Add how many points?: "))
        if how_many <= points:
            type += how_many
    return type

add_points(attributes)

Some context. The code above is a piece of my character creator program I am building. I have the entire program written, however I want to use a function rathre than typing a bunch of loops. Trying to make my code more concise and Pythonic.

I cannot figure out how to take the value of whatever key is chosen by the user and add x amount of points (also chosen by the user) to the value assigned to said key.

Any help would be greatly appreciated!

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
harkbot
  • 1
  • 1

1 Answers1

0

To take the value of a key:

 prevValue = attributes.get(type)

to change the value:

attributes[type] = prevValue + how_many
Miguel
  • 1,295
  • 12
  • 25
  • Ok i've got that part figured out now. I'm confused on why Python is throwing a syntax error when I try to affect the global variable called points with the amount of points selected by the user. If I use how_many = int(input("Add how many points?: ")) if how_many <= global points: that throws a syntax error referring specifically to the use of global. Any ideas? – harkbot Jul 29 '18 at 17:03
  • On the line after the function declaration, add a line which says `global points`. Then you can simply reference the global variable anywhere within this function by just writing `points`. – Damodar Dahal Jul 29 '18 at 21:41