Need help fixing a game I made in Python
<p>I have been set a task to make a fruit machine game in python, however I am facing a small problem, it involves a variable. it is saying that I have referenced the variable before it's assignment, even though I have assigned it. It seems to be reading it as a local variable instead of a global variable. How do i fix this.</p>

<p>This is the part causing the most trouble </p>

<pre><code>Credit = 1

def main(): #the main program
    Credit = Credit - 0.20
    print("Credit remaining = " + Credit) #tells the player the amount of credit remaining
    print("\n *** The Wheel Spins... *** \n") #Spinning the wheel
    print(input("\n (press enter to continue) \n"))
</code></pre>

<h1>error message</h1>

<pre><code>line 19, in main
   Credit = Credit - 0.20
UnboundLocalError: local variable 'Credit' referenced before assignment
</code></pre>

<p>The answer to <a href="http://stackoverflow.com/questions/10588317/python-function-global-variables">this question</a> might help you (copy-pasted below).</p>

<blockquote>
  <p>If you want to simply access a global variable you just use its name. However to change its value you need to use the global keyword.</p>
  
  <p>E.g.</p>
</blockquote>

<pre><code>global someVar
someVar = 55
</code></pre>

<blockquote>
  <p>This would change the value of the global variable to 55. Otherwise it would just assign 55 to a local variable.</p>
  
  <p>The order of function definition listings doesn't matter (assuming they don't refer to each other in some way), the order they are called does.</p>
</blockquote>

<p>You both read <strong>and</strong> change the value of <code>Credit</code> you need to rewrite your code into something along the lines of:</p>

<pre><code>def main(): #the main program(edited)
    global Credit
    Credit = Credit - 0.20
</code></pre>

python pygame global-variables local-variables