UnboundLocalErrors: variables reference before assignment? Need Help! - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: UnboundLocalErrors: variables reference before assignment? Need Help! (/thread-32825.html) |
UnboundLocalErrors: variables reference before assignment? Need Help! - sirLancelot - Mar-08-2021 Using Python Version 3.9.1 on Windows 10. I'm a novice at Python and practicing by making a simple text-based zombie survival game.In its current build, you define your character attributes before fighting off a never-ending horde of zombies. However, I've come across a couple of errors: one when trying to adjust my in-game stats and when I start the game. Current game code # Zombie Survival Sim import random def new_player(): level = 1 xp = 0 next_lv = 10 * level max_health = 10 attack = 10 agility = 10 aim = 10 defence = 10 points = 50 choice = None while choice != "quit": print( """ YOUR CURRENT STATS ################## """) print(" health: \t\t", max_health, "\t Increase amount of health.") print(" attack: \t\t", attack, "\t Increases attack damage.") print(" aim: \t\t\t", aim, "\t Increase weapon accuracy.") print(" agility: \t\t", agility, "\t Increase % to dodge enemy attacks.") print(" defence: \t\t", defence, "\t Reduce damage from enemy attacks.") print(" Points left: \t\t", points, "\n") print("When you are done, type \"quit\".") choice = input("Choose which stat to change: ") if choice == "quit": print("All done.") # health elif choice == "health": change = None while not change: change = int(input("How much do you want to change your health? (You can remove points by entering a minus number) : ")) if change > points: print("No. You don't have enough points to spend.") change = None else: health += change if health < 10: health -= change print("Nope. You can't decrease your health any further than 10.") change = None else: print("Your health has been changed by", change) points -= change # attack elif choice == "attack": change = None while not change: change = int(input("How much do you want to change your attack? (You can remove points by entering a minus number) : ")) if change > points: print("No. You don't have enough points to spend.") change = None else: attack += change if attack < 10: attack -= change print("Nope. You can't decrease your attack any further than 10.") change = None else: print("Your attack has been changed by", change) points -= change # aim elif choice == "aim": change = None while not change: change = int(input("How much do you want to change your aim? (You can remove points by entering a minus number) : ")) if change > points: print("No. You don't have enough points to spend.") change = None else: aim += change if aim < 10: aim -= change print("Nope. You can't decrease your aim any further than 10.") change = None else: print("Your aim has been changed by", change) points -= change # agility elif choice == "agility": change = None while not change: change = int(input("How much do you want to change your agility? (You can remove points by entering a minus number) : ")) if change > points: print("No. You don't have enough points to spend.") change = None else: agility += change if agility < 10: agility -= change print("Nope. You can't decrease your agility any further than 10.") change = None else: print("Your agility has been changed by", change) points -= change # defence elif choice == "defence": change = None while not change: change = int(input("How much do you want to change your defence? (You can remove points by entering a minus number) : ")) if change > points: print("No. You don't have enough points to spend.") change = None else: defence += change if defence < 10: defence -= change print("Nope. You can't decrease your defence any further than 10.") change = None else: print("Your defence has been changed by", change) points -= change else: print("Invalid choice.") def level_up(): health += random.randint(1,10) attack += random.randint(1,10) agility += random.randint(1,10) aim += random.randint(1,10) defence += random.randint(1,10) print("Health: ", health) print("Attack: ", attack) print("Aim: ", aim) print("Agility: ", agility) print("Defence: ", defence) def zombie_check(zombie_health): if zombie_health <= 0: message = "The zombie falls dead." xp += zombie_xp else: message = "The zombie is still standing." return message def check_criteria(player,enemy): criteria = player - enemy great_criteria = criteria / 2 critical_criteria = criteria / 10 perfect_criteria = criteria / 100 return criteria, great_criteria, critical_criteria, perfect_criteria def phase_defend(): print("\nZombies attack!") zombie_health = 0 while health > 0: if zombie_health <= 0: print("\nA zombie appears!") zombie_health = random.randrange(10*level) zombie_aim = random.randrange(10*level) zombie_attack = random.randrange(10*level) zombie_agility = random.randrange(10*level) zombie_defence = random.randrange(10*level) zombie_paces = random.randrange(4) zombie_xp = (zombie_health + zombie_aim + zombie_atack + zombie_agility + zombie_defence) / 10 while zombie_health > 0: if zombies_paces <= 0: print("\nThe zombie lunges to grab you.") success, great, critical, perfect = check_criteria(agility,zombie_aim) roll = random.randint(1,100) if roll <= perfect: print("You nimbly dodge the zombie and put some distance between you and it!") zombie_paces += 1 elif roll <= critical: print("You nimbly dodge the zombie and put some distance between you and it!") zombie_paces += 1 elif roll <= great: print("You nimbly dodge the zombie and put some distance between you and it!") zombie_paces += 1 elif roll <= success: print("You manage to avoid its grasp!") else: print("The zombie grabs hold and bites into your flesh!") damage = zombie_attack - defence health -= damage print("You have", health, "health left.") else: print("The zombie shambles towards you. It is now", zombie_paces, "paces closer to you.") zombie_paces -= 1 print("\nYou fire a shot at the incoming zombie...") success, great, critical, perfect = check_criteria(aim,zombie_agility) roll = random.randint(1,100) if roll <= perfect: print("You land a shot square between the zombies eyes, splattering its head open!") zombie_health = 0 status = zombie_check(zombie_health) print(status) elif roll <= critical: print("You shot the zombie in the head, causing severe damage!") damage = attack - zombie_defence zombie_health -= damage * 4 status = zombie_check(zombie_health) print(status) elif roll <= great: print("You shot the zombie in a vital organ, causing greater damage!") damage = attack - zombie_defence zombie_health -= damage * 2 status = zombie_check(zombie_health) print(status) elif roll <= success: print("You land a shot and hit the zombie.") damage = attack - zombie_defence zombie_health -= damage status = zombie_check(zombie_health) print(status) else: print("You missed!") if xp >= next_lv: level += 1 print("\nLevel up! You are now at level", level, "!") level_up() if health <= 0: print("\nYou succumb to your wounds and join the undead...") new_player() phase_defend() input("Press ENTER to exit.")First error: When I try to enter an integer value to spend points to increase or decrease an in-game stat.
Second Error:Occurs when I enter "quit" in the character creation stage and start the game.
RE: UnboundLocalErrors: variables reference before assignment? Need Help! - deanhystad - Mar-08-2021 There is no variable named "health" defined in the function "new_player()" when you execute commands like this: health -= changeIs health defined somewhere else? If so, you need to either pass in the health value or use the global declaration to tell Python that the variable is defined outside of "new_player()". I'm not going to even look at the second problem as it is probably the same as the first. |