title: Redefining a variable inside a loop
body: <p>I am having a problem with getting an 'undefined' error in my code or, my variable being incorrectly redefined. I am trying to redefine a variable that is inside a loop each time the loop is executed. But the problem is that either (when my variable is defined outside the loop) i get a 'variable is undefined' error, or the variable does not change and/or is reset to zero when the loop is reinitialized.</p>
  
  <pre><code>def game():
      scorePlayer = 0
      scoreAI = 0 #If I define it here I get the latter of the two errors explained.
      number = random.randint(1, 6)
      print ("Your score is ", scorePlayer, ". Your opponent's score is ", scoreAI, ".") #This is where it tells me it is referenced before defined if I define outside the loop.
      print (" ")
      print ("Rolling...")
      print (" ")
      time.sleep(2)
      print ("You have rolled a ", number, ".")
      print (" ")
      print ("Player, would you like to hold (enter 'hold') or roll (enter 'roll')?")
      print (" ")
      decide = raw_input(" ")
      if decide == 'hold':
          print (" ")
          scorePlayer = tempScorePlayer + scorePlayer
          gameAI()
      elif decide == 'roll': #changed to elif
          print (" ")
          if number == 1:
              print ("You have rolled a 1. You will not gain points from this turn.")
              print (" ")
              tempScorePlayer = 0
              gameAI()
          elif number &gt;= 2: #changed to elif
              print (" ")
              tempScorePlayer = number + tempScorePlayer
              game()
      if scorePlayer &gt;= 100:
          winPlayer()
  </code></pre>
  
  <p>I have tried defining the variables everywhere else so that they wouldn't interfere with the loop and still couldn't get it to work.</p>
  
  <p>Any help would be much appreciated, thanks.</p>