My variable is defined but python is saying it isn't?
<p>I keep getting an error telling me that the name <code>hourly_pay</code> is not defined, but I have it defined inside the <code>main</code> function. </p>

<p>I'm a beginner as I've just started class but to me it looks like it should be working:</p>

<pre><code>commission_pay_amount = .05
income_taxes = .25
Pay_per_hour = 7.50

def main():
    display_message()

    hourly_pay = float(input('Please enter amount of hours worked: '))

    commission_pay = hourly_pay * commission_pay_amount
    gross_pay = hourly_pay + commission_pay 
    witholding_amount = gross_pay * income_taxes  
    hourly_paying = Pay_per_hour * hourly_pay
    net_pay = gross_pay - witholding_amount

    display_results()

def display_message():
    print('This program is used to calculate')
    print('the hourly pay, commission amount,')
    print('the gross pay, the withholding amount,')
    print('and the net pay amount')
    print()

def display_results():
    print('The hourly pay is $', format(hourly_pay, ',.2f'))
    print('The commission amount is $', format(commission_pay, ',.2f'))
    print('The gross pay is $', format(gross_pay, ',.2f'))
    print('The witholding amount is $', format(witholding_amount, ',.2f'))
    print('The net pay is $', format(net_pay, ',.2f'))

main()
</code></pre>

<p>In python (in contrast to JavaScript), variables are locally scoped by default. This means that the variables are only accessible inside the function they are defined in. This behaviour can be overridden, but usually <a href="https://stackoverflow.com/questions/19158339/why-are-global-variables-evil">you do not want that</a>.</p>

<p>To illustrate the difference, take a look at this python transcript:</p>

<pre><code>&gt;&gt;&gt; var1 = "this is global"
&gt;&gt;&gt; def foo():
...   var1 = "this is local"
...   print(var1)
... 
&gt;&gt;&gt; print(var1)
this is global
&gt;&gt;&gt; foo()
this is local
&gt;&gt;&gt; print(var1)
this is global
</code></pre>

<p>As you can see, even though <code>var1</code> is assigned to in the <code>foo()</code> function, the value of the <code>var1</code> name does not change in the global scope. If we had not defined <code>var1</code> globally at all, the two <code>print(var1)</code> calls outside <code>foo()</code> would fail with a NameError, just like your code does.</p>

<p>The ultimate solution to your problem is to either handle output in the <code>main()</code> function, or pass the values to the <code>display_results()</code> function (the latter is generally preferred, keep logic and output separated):</p>

<pre><code>def main():
    display_message()
    hourly_pay = float(input('Please enter amount of hours worked: '))
    commission_pay = hourly_pay * commission_pay_amount
    gross_pay = hourly_pay + commission_pay 
    witholding_amount = gross_pay * income_taxes  
    hourly_paying = Pay_per_hour * hourly_pay
    net_pay = gross_pay - witholding_amount

    display_results(hourly_pay, commission_pay, gross_pay, 
                    withholding_amount, net_pay)

def display_message():
    print('This program is used to calculate')
    print('the hourly pay, commission amount,')
    print('the gross pay, the withholding amount,')
    print('and the net pay amount')
    print()

def display_results(hourly_pay, commission_pay, gross_pay, 
                    withholding_amount, net_pay):
    print('The hourly pay is $', format(hourly_paying, ',.2f'))
    print('The commission amount is $', format(commission_pay, ',.2f'))
    print('The gross pay is $', format(gross_pay, ',.2f'))
    print('The witholding amount is $', format(witholding_amount, ',.2f'))
    print('The net pay is $', format(net_pay, ',.2f'))
</code></pre>

<p><a href="https://docs.python.org/3/tutorial/controlflow.html#defining-functions" rel="nofollow">The official Python tutorial also has a few words on function scopes</a> (emphasis mine):</p>

<blockquote>
  <p>More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, <strong>global variables cannot be directly assigned a value within a function</strong> (unless named in a <code>global</code> statement), although they may be referenced.</p>
</blockquote>

python variables scope nameerror