-2

That's my codes. When it runs it always shows:

nameerror: global name "weapon_choice" is not defined.

But i have already put in the weapon_choice. Why?

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

I thought when it runs,at the very beginning,it have already ask the inputer to input the weapon_choice.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Quan Wang
  • 7
  • 3
  • 1
    Possible duplicate of [Python scoping problem](http://stackoverflow.com/questions/1195577/python-scoping-problem) – c2huc2hu Mar 06 '17 at 03:41

2 Answers2

0

You should be getting: NameError: name 'weapons' is not defined

while True:
   weapon_choice = raw_input(">")

is called before start() hence the error. Try enclosing code blocks in functions for better readability and debugging.

Update: I believe your code indentation above is not correct. It works fine, and as expected without the error you mentioned.

jimseeve
  • 141
  • 1
  • 7
0

First, it looks like you have an indention error. Everything from

while True:

to

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

should be tabbed over one level so that it is inside the start function.


There is also a scope issue In your lift function: weapon_choice is not defined.

You should pass weapon_choice as a parameter to the lift function when calling it:

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

After that, you just need to write the functions street_1, friends_axe, and kill_frinends--then you're done!

Nelson
  • 922
  • 1
  • 9
  • 23