【问题标题】:unboundLocalError: local variable 'arm' referenced before assignment?unboundLocalError:分配前引用了局部变量“arm”?
【发布时间】:2017-02-13 07:06:43
【问题描述】:

大家好,我是 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()

【问题讨论】:

    标签: python


    【解决方案1】:

    问题是当你这样做时:

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

    你还没有定义什么是 arm.. 你只在这一行定义了 arm

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

    它在elif 的内部范围内——这意味着如果它没有达到那个点,那么arm 确实是一个在对其进行任何操作之前未分配的局部变量。

    也许你的意思是这些if arm == 1: ... 在上面elif 的代码中我无法分辨,但我认为你应该看看如何更改你的代码以包含更少的意大利面条代码.. 分为函数和类。

    【讨论】:

      【解决方案2】:

      您已经在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.
      

      【讨论】:

        猜你喜欢
        • 2020-08-13
        • 1970-01-01
        • 2017-08-10
        • 2020-01-16
        • 2019-12-05
        • 2017-09-19
        • 2020-09-26
        • 2022-01-02
        • 2019-03-22
        相关资源
        最近更新 更多