2017-03-06 18 views
-2

これは私のコードです。実行時には常に次のように表示されます:常に:グローバル名が定義されていません

nameerror: global name "weapon_choice" is not defined.

しかし、私はすでにweapon_choiceを入れています。どうして?

from sys import exit 

def dead(why): 
    print why,"YOU ARE EATEN BY ZOMBIES!" 
    exit(0) 

def start(): 
    print "You wake up alone in a room.A crowed of corpses around you." 
    print "You find some tools beside maybe helpful." 
    print "What will you get?" 

    weapons = ['axe', 'bat', 'knife', 'pistol'] 
    print weapons 

while True: 
    weapon_choice = raw_input(">") 

    if weapon_choice in weapons: 
     print "Zombies walk towards you, now it's in urgency!" 
     print "You can't deal with them.You must flee!" 
     print "You find door and window.Where you will go?" 

     paths = raw_input(">") 

     if paths == "window": 
      dead("You fall down to the ground.") 
     elif paths == "door": 
      lift() 
     else: 
      dead("oops!") 

    else: 
     print "Can't understand what you say." 

def lift(): 
    print "You can't recognize the number in the lift." 
    print "Get out of the lift or tap a floor whatever. " 

    floor_choice = raw_input(">") 

    if floor_choice == "get out" and weapon_choice != "pistol": 
     dead("Outside full of zombies.You have nowhere to go.") 

    elif floor_choice == "get out" and weapon_choice == "pistol": 
     print "WOW!!!YOU KILL ALL THE ZOMBIES!!" 
     print "you get back to the bed for another sleep." 
     start() 
    elif floor_choice == "tap a floor" and weapon_choice == "axe": 
     street_1() 
    elif floor_choice == "tap a floor" and weapon_choice == "bat": 
     friends_axe() 
    elif floor_choice == "tap a floor" and weapon_choice == "pistol": 
     kill_frinends() 

    else: 
     print "Can't understand what you say." 


start() 

私はそれを実行したときに、一番最初に、それはすでに入力にweapon_choiceをinputerをお願いしてきたと思いました。あなたは取得する必要があります

+1

[Pythonスコープ問題]の可能な複製(http://stackoverflow.com/questions/1195577/python-scoping-problem) – user3080953

答えて

0

まず、インデントエラーがあるようです。それはstart関数内になるように

while True: 

から

else: 
    print "Can't understand what you say." 

にすべてが一段階上のタブべきです。


あなたlift機能でも、スコープの問題があります:weapon_choiceが定義されていません。

あなたはそれを呼び出すときlift関数にパラメータとしてweapon_choiceを渡す必要があります、その後

def start(): 
    print "You wake up alone in a room.A crowed of corpses around you." 
    print "You find some tools beside maybe helpful." 
    print "What will you get?" 

    weapons = ['axe', 'bat', 'knife', 'pistol'] 
    print weapons 

    while True: 
     # your while loop code here 

     elif paths == "door": 
      lift(weapon_choice) 

     # the rest of your while loop code here 

def lift(weapon_choice): 
    # your lift function code here 

を、あなただけの機能street_1を記述する必要がfriends_axe、およびkill_frinends --thenます」再完了!

0

NameError: name 'weapons' is not defined

while True: 
    weapon_choice = raw_input(">") 

が故にstart()エラーの前に呼び出されます。コードブロックを関数に囲み、読みやすく、デバッグしやすくしてください。

更新:上記のコードのインデントが正しくないと思われます。それはうまく動作し、あなたが言及したエラーなしで期待どおりに動作します。

関連する問題