Python Forum
local variable 'spider_1_health' referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
local variable 'spider_1_health' referenced before assignment
#1
Hello!
I've been coding a text based game which includes many variables, I have condensed the code more to make it easier to debug. I have a problem with the variable spider_1_health, where it says:
local variable 'spider_1_health' referenced before assignment

if anyone knows why this is happening, I would greatly appreciate it! Thanks!

#imports here
import random
import time
import sys
import string

string.ascii_letters

#attack damage, crit chance and armor rating
armor_number = 0
attack_damage = 0
crit_chance = 1
dragon_damage = 1

#all the variables - lets go!
health = 20
coins = 700

#skill levels
combat_skill = 0
mining_skill = 0
farming_skill = 0

spider_1_health = random.randint(12,25)

def layer():
    layer = 0
    return layer

spider_1_health = random.randint(12,25)

layer = layer()

def console_main():
    global console_command
    #PUT ALL GLOBAL VARIABLES HERE:

    global shop_buy_options
    global shop_forge_options
    global inventory
    global coins
    global combat_skill
    global mining_skill
    global farming_skill
    global sword_equipped
    global helmet_equipped
    global chestplate_equipped
    global leggings_equipped
    global boots_equipped
    global crit_chance
    global dragon_damage
    global health
    #global spider_1_health
        
    
    if 1==1:
        print("Welcome to the Spider's Den. Here be monsters! There are skeletons, spiders, and all sorts of mystical creatures in this mound of death. The Spider Queen lurks at the bottom most cave.")
        print("Here are your options:")
        def spider_den_console():
            print("""
            /down: Enter the first layer of the mound, or go down one layer!
            /up: Start moving out of this evil place!
            /r: If outside, return to the base.
        
        
            """)
            spider_console = input("Choice: ")
            global layer
            if spider_console == "/up":
                time.sleep(4)
                
                if layer == 0:
                    print("Unable to perform action. You are at the surface.")


                else:
                    layer = layer - 1
                    print("You are in room ", layer)


            if spider_console == "/down":
                time.sleep(3)

                if layer == 9:
                    print("Unable to perform action! In the Queen's Den!")

                if layer < 9:
                    layer = layer + 1
                    print("You are in room ", layer)



            if spider_console == "/r":
                if layer == 0:
                    location.clear()
                    location.append("base")
                if layer > 0:
                    print("Not on surface!")
                    
        def spiders_den_1():
            global attack_damage
            global spider_1_health
            global health
            ability = random.randint(11,20)
            true_ability = (ability*layer)


            if true_ability < 11:
                spider_den_console()
            if 10 < true_ability < 30:
                print("A Lvl. 1 Spider is moving to attack!")
                #spider_1_health = random.randint(12,25)

                def you_attack():
                    spider_1_rand_letter = random.choice(string.ascii_letters)

                    first_attack_spider_1_before = time.time()
                    print("Get the first hit! click letter ")

                    random_spider_1_time = random.randint(0,6)

                    time.sleep(random_spider_1_time)

                    attack_spider_1 = input(spider_1_rand_letter)
                    first_attack_spider_1_after = time.time()
                
                    spider_1_attack_time = first_attack_spider_1_after - first_attack_spider_1_before

                    spider_1_critical = 0

                    rand_crit_spider_1 = random.randint(0,100)
                    crit_possible = rand_crit_spider_1*crit_chance

                    if crit_possible > 85:
                        spider_1_critical = 1
                        print("Critical Hit!")

                    if spider_1_rand_letter == attack_spider_1:
                        print("Attack Successful!")
                        total_damage_spider_1 = (((1/spider_1_attack_time)**1.15)*attack_damage)*(spider_1_critical + 1)
                        print("You did ",total_damage_spider_1," damgage to the level one spider.")

                        spider_1_health = spider_1_health - total_damage_spider_1

                def spider_1_check_dead():
                    if spider_1_health <= 0:
                        print("You killed the level one spider!")
                        combat_skill = combat_skill + 5

                        spider_1_loot = random.randint(1,100)
                        
                        if spider_1_loot < 30:
                            print("No loot received from the spider. Combat level has increased by 5.")
                            
                        if 29 < spider_1_loot < 90:
                            print("You found Ordinary Silk!")
                            inventory.append("Ordinary Silk")
                            #make sure to do something with this!

                        if 89 < spider_loot_1 < 98:
                            print("You found the Rare Spider Mandible! (It may be used as a weapon)")
                            inventory.append("Rare Spider Mandible")

                        if 97 < spider_loot_1:
                            print("You found a Spider Egg and 1 Hardened Diamond!")
                            inventory.appned("Spider Egg")
                            inventory.append("Hardened Diamond")

                def spider_1_attack():             
                    print("The Spider is still alive and is attacking you!")
                    spider_1_damage = random.randint(1,7)
                    health = health - spider_1_damage

                    print("Your Health: ", health)
                            

                while spider_1_health > 0:
                    you_attack()

                    if spider_1_health > 0:
                        spider_1_attack()

                    spider_1_check_dead()


        spider_den_console()

        print (layer)

        while layer == 0:
            spider_den_console()


        while layer == 1:
            spiders_den_1()


console_main()

while 1==1:
    console_main()
Reply
#2
i would learn classes and stop using global keyword completely. I would stick to definition of a function to global scope and not in an if condition. All this is asking for trouble.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Error: variable referenced before assignment DotGuy 2 2,749 Jun-19-2020, 02:28 PM
Last Post: DotGuy
  I dont know where my local variable has gone Help_me_Please 4 2,923 Apr-04-2020, 05:25 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020