简体   繁体   中英

unboundLocalError: local variable 'arm' referenced before assignment?

Hi everyone I'm a beginner in python and I'm trying to make my own text rpg game. I have made a method for the hero to shop in the store but for some reason I'm getting this error every time I reach the shop :

nboundLocalError: local variable 'arm' referenced before assignment

Can someone explain to me what this means and how I can fix it? thanks

 def shop():
        dagger = ('Dagger', 0, 5)
        sword = ('Sword', 0, 10)
        leather_hide = ('Leather Hide', 5, 0)

        if IsShopLocked == True:
            print("The shop is locked!\nPlease go back and continue your adventure!")
        else:
            print()
            print("Welcome to the Larkville shop! What would you like to buy?\n1. Weapons\n2. armor\n3. Go back")
            selection = int(input("Enter a value: "))

        if selection == 1:

                print("Weapons shop")
                print("1. Bronze Dagger: $7\n2. Bronze Sword: $50 3.Rusty Sword $60")
                wpnselection= int(input("Enter a value: "))

        elif wpnselection == 1:

                if hero.ac<20:
                    print("You donthave enough gold to buy this yet ")
                main()
        else:


                    hero.damage += 10
                    hero.ac -= 20
                    print("strength increased to: {}".format(hero.damage))
                    main()

        if wpnselection == 2:
                if hero.ac<50:
                    print("You dont have enough gold to buy this yet...")
                    main()
                else:


                    hero.damage += 16
                    hero.ac -= 50
                    print("strength increased to: {}".format(hero.damage))
                    main()


        elif wpnselection == 3:
               if hero.ac<60:
                    print("You dont have enough gold to buy this yet...")
                    main()
               else:


                    hero.damage += 28
                    hero.ac -= 60
                    print("strength increased to: {}".format(hero.damage))
                    main()

        elif selection == 2:

                print ("Armor Shop")
                print ("1. Leather hide 20$\n2. warmogs armor 30$")
                arm = int(input("enter a value: "))

        if arm == 1:

                if hero.ac<20:
                    print("You dont have enough gold!")
                main()
        else:

                hero.hp += 20
                hero.ac -= 20
                print("Health increased to: {}".format(hero.health))

        if arm == 2:

                    if hero.ac<30:
                     print("You dont have enough gold!")
        main()
        if hero.ac>30:
                    leather_hide = Item('Leather Hide', 5, 0)
                    IsLeatherHideEquipped = True
                    hero.hp += 20
                    hero.ac -= 20
                    print("Health increased to: {}".format(hero.health))


        elif selection == 3:
           main()

Problem is that when you do:

if arm == 1:
    # code
if arm == 2:
    # code

you have not defined what arm is.. you only define arm in this line:

arm = int(input("enter a value: "))

Which is in an inner scope of the elif - which means that if it doesn't reach that point then arm is indeed a local variable that was not assigned before doing anything with it.

Maybe what you meant to do is that these if arm == 1: ... in the scode of the elif above I can't tell but I think you should see how you can change your code to contain less spagetti code.. deviding into functions and maybe classes.

You have declared variable arm inside the elif (inner scope) and you are trying to use that variable out of that scope. Here, Same thing is happening with another variable selection .

If control would not reach up to that conditions, your variables would be undefined.

You can first declare these variable with None

def shop():
    dagger = ('Dagger', 0, 5)
    sword = ('Sword', 0, 10)
    leather_hide = ('Leather Hide', 5, 0)
    selection=None
    arm=None
    #rest of the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM