簡體   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