Question: For example, let's say I'm trying to create a wallet system in python where money can be add to and taken out of. I try this code here:

```
balance = 0
def addmoney(x):
    x += balance
addmoney(10000)
print(balance)

```

but it just gave me 0.
so then I tried this:

```
def addmoney(x):
    balance = 0 
    balance += x

```

And I realized that this would set the money back to 0 every time the user adds money, which I didn't want. Is there a solution to this?

Answer: 
You could declare the *balance* variable as global inside the function.

```
balance = 0
def addmoney(x):
    global balance
    balance += x
addmoney(10000)
print(balance)

```
In Python, you must use the **global** keyword when accessing global variables from within a function. See [here](https://www.w3schools.com/python/python_variables_global.asp).

One way to keep track of the changes is to store the previous values in a list outside the function. Again, using the **global** keyword

Also your logic for the balance is backwards. You should be adding x to the balance.

```
balance = 0
historyLst = []
def addmoney(x):
    global balance
    global historyLst
    historyLst.append(balance)
    balance += x

addmoney(10000)
print(balance)

```