2017-02-13 7 views
1

こんにちは皆私はPythonの初心者で、私は自分自身のテキストRPGゲームを作ろうとしています。私はヒーローが店で買い物をするが、私は毎回このエラーを取得しています何らかの理由で、私はお店に到達するための方法を作った:unboundLocalError:ローカル変数 'arm'が代入の前に参照されていますか?

nboundLocalError: local variable 'arm' referenced before assignment

誰かがこれは何を意味するのか私には説明できて、私はどのように修正することができます それ?

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

あなたは..あなたが唯一のこの行で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() 

答えて

2

問題は、あなたがやるときということであるである

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

elifの内部スコープ - つまり、そのポイントに達していない場合は、armは実際に何かを実行する前に割り当てられていないローカル変数ですああ。多分関数とにdeviding ..

は、たぶん、あなたが何を意図してI上記elifのSCODEにおけるこれらif arm == 1: ...が言うことができないということですが、私はあなたが少なくスパゲティプログラムを含むようにコードを変更することができますどのように見るべきだと思いますクラス。

+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. 
関連する問題