0

I'm making a sort of text based RPG for fun and I'm having some trouble adding to the "balance" variable to give the player more money.

balance = int(0)

def step_result(balance):
    result = random.randint(1,5)
    if result == 1:
        time.sleep(0.5)
        print("You come across a friendly merchant who gives you 50 gold pieces as a token of friendship")
        balance = balance + 50
        time.sleep(1)

Later on in the code, I use print(balance) to show the balance but it still displays as 0. Any ideas?

1 Answers1

0

Try this, global should fix your issue

balance = int(0)

def step_result(balance):
    result = random.randint(1,5)
    global balance
    if result == 1:
        time.sleep(0.5)
        print("You come across a friendly merchant who gives you 50 gold pieces as a token of friendship")
        balance = balance + 50
        time.sleep(1)
Shold
  • 75
  • 5