Programming a function that saves and returns values in python
<p>I am currently experimenting with Python and programming a little text-adventure. In my game the player has certain properties like hp, attack damage and inventory slots for items.
I want to be able to call these properties from everywhere in my code. For that I created a function that receives three values: </p>

<p>"edit": to specify if a variable should be edited</p>

<p>"info_id": to specify which variable should be accessed</p>

<p>"value": the new value for the variable</p>

<p>This is what it looks like in my code:</p>

<pre><code>def player_info(edit, info_id, value):

  if edit == 1:
  ##function wants to edit value
      if info_id == 1:
          player_hp = value
          print ("Assigned hp to: ", player_hp) 
          ##the "prints" are just to check if the asignments work -&gt; they do
          return player_hp

      elif info_id == 2:
          player_attack = value
          print ("Assigned attack to: ", player_attack)
          return player_attack
      elif info_id == 3:
          item_1 = value
          return item_1
      elif info_id == 4:
          item_2 = value
          return item_2
       elif info_id == 5:
          item_3 = value

  elif edit == 0:
  ##function wants to retrieve value
      if info_id == 1:
          return player_hp
      elif info_id == 2:
          return player_attack
      elif info_id == 3:
          return item_1
      elif info_id == 4:
          return item_2
      elif info_id == 5:
          return item_3
</code></pre>

<p>There are actually 10 item slots (going up to info_id==13) but they are all the same anyway.</p>

<p>I define all variables at the beginning of my code:</p>

<pre><code>  player_info(1,1,20)
  player_info(1,2,5)
  n=3
  while n&lt;=13:
      player_info(1,n,0)
      n=n+1
##items are not fully implemented yet so I define the item slots as 0
</code></pre>

<p>The definition works, I can tell because of the control "print" I implemented in the code. Still when I call a variable, e.g. the health like this:</p>

<pre><code>player_info(0,1,0)
</code></pre>

<p>I get an error:</p>

<pre><code>local variable 'player_hp' referenced before assignment
</code></pre>

<p>Does the function not save the variable properly? Or what is the problem?</p>

<p><strong>Is there a better way to save variables? Are global variables the way to go in this case?</strong></p>

<p>Thanks for the help!</p>

<p>First of all, your error is caused because of retrieving a variable that is not assigned - that just doesn't work. When you edit <code>player_hp</code>, it's not stored anywhere. you are returning it to the function that called it and not assigning it to anything. It just gets lost.</p>

<p>Second of all, you should really indent with 4 spaces (or tabs) - it's much more readable than 2 spaces. Not only for you, but for anyone trying to help too.</p>

<p>And lastly, the proper way to go about this would be to learn about classes. Global variables should never be used in python, only in special cases, or when you are learning, but just skip ahead to the class.</p>

<p>You should create something like</p>

<pre><code>class Player:

    def __init__(self):
        self.hp = 20  # or another starting hp
        self.attack = 3  # or another starting attack
        self.inventory = []
</code></pre>

<p>Then you can just create an instance of Player class and pass it to the functions where it's relevant</p>

<pre><code>player1 = Player()
print(player1.hp) # Prints out player's hp
player1.hp -= 5  # Remove 5 hp from the player. Tip: Use method to do this so that it can check if it reaches 0 or max etc.
player1.inventory.append("axe")
print(player1.inventory[0]) #  Prints out axe, learn about lists, or use dictionary, or another class if you want this not to be indexed like a list
</code></pre>

python function variables save call