Question

I'm very new to programming (so sorry if I don't present this problem right). This is from LPTHW Exercise 36:

My Error:

Traceback (most recent call last):
  File "ex36.py", line 329, in <module>
    start()
  File "ex36.py", line 149, in start
    arena()
  File "ex36.py", line 161, in arena
    if stealth == True:
NameError: global name 'stealth' is not defined

My Assumption: I thought 'stealth' was defined in the previous function, start(), but the definition didn't carry over to arena(). How do I fix it, and why doesn't 'stealth' from 1 function carry over to another function?

My Code (text-based game in progress):

    from sys import argv

    script, enemy = argv
    ...
    def start():
        print """ Choose a skill to train in
        """
        stealth = False
        gun = False
        knife = False
        heal = False
        skill = raw_input("> ")

        if 'gun' in skill:
            print """
            """
            gun = True
            skill = gun
        ...
        else:
            dead()

        arena()

    def arena():
        print """ You enter the arena.  Will you:
        hide, hunt for food, or search for water?
            """

        path = raw_input("> ")

        if "hide" in path:
            print """ Hide
            """

            if stealth == True:
                print """ Witness
                """
                witness()
            else:
                battle()
        ...
        else:
            print """ Dead
            """
            dead()
start()

All advice is greatly appreciated. Thank you for your help.

Was it helpful?

Solution

Variables defined locally in one function have local scope and are not automatically accessible within another, disjunct function. You might want to consider passing stealth to arena when called from start, e.g. arena(stealth), and then stealth would be defined as a parameter of arena, i.e.

def arena(stealth):
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top