2

I went through this a couple of times and couldnt fine anything wrong so its probably something over my head. I also apology for what is going to be an assault on your eyes, its my first year of programming and have probably made multiple etiquette errors.

print('Befor we begin, when you are given options, I ask you to type your input as show, failure to do so will break the program and you will lose all of your progress')
def test():
    print('it has worked!')
def stats():
    global attack
    global health
    global mashealth
    global agility
    if race in ('human','Human'):
        maxatk=4
        maxagi=4
        attack = lambda:random.randint(2,maxatk)
        maxhealth = 20
        health=20
        agility = random.randint(maxagi,10)


    elif race in ('Orc','orc'):
        attack = random.randint(3,maxATK)
        maxhealth = 25
        agility = random.radint(maxAGI,10)


def main():
    while True:
        print('What would you like to do')
        print('Rest   Shop   Fight')
        answer=input('-')
        if answer in ('Rest','rest'):
            health=maxhealth
            continue

def character():
    global race
    global name
    global gender
    print('What would you like the name of your character to be?')
    name=input('-')
    print()
    print('What race will your character be?')
    print('Human   Orc   Elf')
    while True:
        race = input('-')
        if race in ('human','Human','Orc','orc','Elf','elf'):
            break
        else:
            print('Not a valid response, try again')
            continue
    print()
    print('What gender is your character')
    gender=input('-')
    print()

def goblin():
    goblinatk=1
    goblinhealth=100
    while True:
        print('You have encountered a goblin, what will you do?')
        do=input('-')
        if do == 'attack':
            damage=attack()
            goblinhealth=goblinhealth-damage
            print('You did',damage,'damage to the goblin')
            print('The goblin has',goblinhealth,'hp')
        goblinatk=lambda:random.randint(3,10)
        health=health-goblinatk
        print('the goblin did',goblinatk,'to you')
        print('you have',health,'hp')
        if goblinhealth <0:
            print('The goblin has died')
            break
        if health <0:
            print('you have died')
            break






character()
stats()
goblin()
test()

The error is here

Traceback (most recent call last):
  File "H:\16CoFrega\Code\Python\GAME.py", line 255, in <module>
    goblin()
  File "H:\16CoFrega\Code\Python\GAME.py", line 238, in goblin
    health=health-goblinatk
UnboundLocalError: local variable 'health' referenced before assignment

2 Answers2

2

You need to specify the health as a global variable, otherwise it would be considered as a local variable, since you assign to it inside the function. Example -

def goblin():
    global health
    goblinatk=1
    goblinhealth=100
Sharon Dwilif K
  • 1,218
  • 9
  • 17
2
def goblin():
    ............
    health=health-goblinatk
    ............

Look at this function definition. Your function doesn't know what health is, so it won't allow you to subscript something from health.

So, somehow function has to recognize what is health. Two most common way:

  • First way, Declare health as a global variable. Now health can be recognized globally. But this is not the best way, as dealing with global variables are hard and error-prone and you are already handling too many global variables. So, i won't recommend it. I would rather suggest you to replace all the global variables with method 2. To understand why I am telling this, Read This Question

  • Second way, the recommended way, is to pass the health variable as the parameter of the function and at the end, return it from the function.

Like this:

def goblin(health):
    ............
    health=health-goblinatk
    ............
    return health

If you are already returning something, don't worry. With python, you can return more than one variable as a tuple.

return statement:

return a,b,c

calling statement:

a,b,c = func()

Hope this helps :)

Community
  • 1
  • 1
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57