2017-02-13 118 views
1

大家好我是一名Python初學者,我正在嘗試製作自己的文本RPG遊戲。我已經爲英雄在店裏購物,但由於某種原因,每次我得到這個錯誤我到店裏的方法:unboundLocalError:賦值之前引用的局部變量'arm'?

nboundLocalError: local variable 'arm' referenced before assignment

有人能向我解釋這意味着什麼,以及如何我可以修復它?感謝

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

回答

2

問題是,當你這樣做:

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

您還沒有定義什麼手臂..你只在這一行定義arm

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

這是一個elif的內部範圍 - 這意味着如果它沒有達到那個點,那麼arm確實是一個局部變量,在做任何事情之前都沒有被分配h it。

也許你想要做的是,這些if arm == 1: ...在上述elif scode的我不能告訴,但我認爲你應該看看你如何可以改變你的代碼以包含更少的spagetti代碼..分爲功能和可能類。

+0

@Andrew - 這是否幫助你解決問題嗎? –

0

您已在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. 
相關問題