Variable scope in python code
<pre><code>from sys import exit

def gold_room():
    print "This room is full of gold.  How much do you take?"

    choice = raw_input("&gt; ")
    if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number.")
    if how_much &lt; 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")

def dead(why):
    print why
    exit(0)
</code></pre>

<p>1) How does the <code>dead</code> method is called inside <code>gold_room</code> method as the definition of <code>dead</code> method is below the calling statement?</p>

<p>2) How does the variable <code>how_much</code> accessed outside of its scope? It was declared and initialized inside the indented block -</p>

<pre><code> if "0" in choice or "1" in choice:
        how_much = int(choice)
</code></pre>

<p>And according to my understanding, its scope should be ended here in this block. Then how it is used further in this condition - <code>if how_much &lt; 50</code> ?</p>

<p>There is no "dead" method built into Python. The reason why the interpreter tells you that it is not defined is because you haven't defined it.</p>

<p>Python does not have block scope. Variables defined inside a function are visible from that point on, until the end of the function.</p>

<p><strong>edit after the first question was changed</strong> It doesn't matter in which order the functions are defined, as long as they are both defined at the time they are <em>called</em>. Presumably, you have some code at the end of that module which calls <code>gold_room</code>, after both functions are defined.</p>

python python-2.7 scope