title: Python Global Variable Not Working
body: <p>I have an extremely long program for a Canasta game that I am writing, in it there is a variable that ends the turn of the player if it equals a certain value. However, the turn repeats itself. </p>
  
  <p>I have read many different articles and posts on forums like this one, but none of these seem to work. I even have a debug system to print the value of the variable before the if statement, and the variable is the right value, but the if statement doesn't see the same value. </p>
  
  <p>I can't give the entire code, as it is extremely long, but I can give the sections that use this variable. I am using Python 2.7.2 and cannot make files separate from the main file to import.</p>
  
  <p>The first line of the program is:</p>
  
  <pre><code>endTurn=1
  </code></pre>
  
  <p>The following function is called when conditions to win the game are met, but I have not been able to test that this works because of the bug.</p>
  
  <pre><code>def winGame(player):
      global endTurn
      gameWinner=player
      gameWinner["score"]+=100
      endTurn=0
  </code></pre>
  
  <p>The following function is called when a move is made. The variable <code>decide</code> is a <code>raw_input()</code> variable.</p>
  
  <pre><code>def move(player,decide):
      global endTurn
      theMove=decide.lower()
      #if player says to end turn
      if theMove=="end":
          #until player discards something
          discarded=0
          while not discarded:
              displayHand(player)
              #ask player for discard
              discard=int(raw_input(" Enter the list number of the card you wish to discard:"))-1
              #if discard ID is in player's hand length
              if not discard&lt;0 and not discard&gt;len(player["hand"])-1:
                  #add card to discard pile and remove from hand
                  discardPile.append(player["hand"][discard])
                  del(player["hand"][discard])
                  discarded=1
          debug("Before changing, endTurn is %s"%str(endTurn))
          endTurn = 0
          debug("After changing, endTurn is %s"%str(endTurn))
      if theMove=="new book":
          newBook(player)
      if theMove=="add to book":
          addBook(player)
  </code></pre>
  
  <p>Here is where the turn should be ended. <code>turn(thePlayer)</code> repeats the function. The debug statement shows the correct value, 0, but the <code>if</code> still reads a 1. The function this is in also has the <code>global endTurn</code> at the top.</p>
  
  <pre><code>debug("If ending turn, endTurn of %s should be 0."%str(endTurn))
  if endTurn==1:
      turn(thePlayer)
  </code></pre>
  
  <p>Any help is greatly appreciated!</p>
  
  <p>EDIT: The code is available at <a href="http://labs.codecademy.com/CV9z#:workspace" rel="nofollow">http://labs.codecademy.com/CV9z#:workspace</a>. I ask that anyone viewing the code does not modify it, so that other people can see the true code.</p>