繁体   English   中英

unboundLocalError:分配前已引用局部变量“ arm”?

[英]unboundLocalError: local variable 'arm' referenced before assignment?

大家好,我是python的初学者,我正在尝试制作自己的文字rpg游戏。 我已经为英雄制作了一种在商店中购物的方法,但是由于某种原因,每次我到达商店时都会遇到此错误:

nboundLocalError:分配前已引用局部变量“ arm”

有人可以向我解释这意味着什么,我该如何解决? 谢谢

 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()

问题是您这样做时:

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

您尚未定义手臂是什么..您仅在此行中定义arm

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

这在elif的内部范围内-这意味着,如果未达到该点,则arm实际上是在对其执行任何操作之前未分配的局部变量。

也许您的意思是说, if arm == 1: ...在上面的elif的代码中我看不出来,但是我认为您应该看到如何更改代码以包含更少的spagetti代码。到函数甚至类中。

您已经在elif (内部作用域)中声明了变量arm ,并且试图在该范围之外使用该变量。 在这里,另一个变量selection也发生了同样的事情。

如果控制无法达到该条件,则您的变量将不确定。

您可以先使用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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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