title: Getting error "local variable referenced before assignment - how to fix?
body: <p>I'm making a gambling program (i know this shouldn't be incredibly hard), and want to have multiple games which will be in subroutines. However, python seems to think my variables are being assigned in strange places.</p>
  
  <p>I am semi-new to subroutines, and still have some issues here and there. Here is what I am working with:</p>
  
  <pre><code># variables and subroutines
  money = 500
  losses = 0
  wins = 0
  wlr = (wins,':',losses)
  egg = True
  
  def fiftyfifty(bet):
      chance = random.randint(0,100)
      if chance &gt; 50:
          losses += 1
          print('You lose!')
          money -= bet
          print('You now have ',money)
          return
      else:
          wins += 1
          print('You win!')
          money += bet
          print('You now have ',money)
          return
  </code></pre>
  
  <p>And here is how it is called:</p>
  
  <pre><code>elif gamechoice == 'fifty fifty':
          print('You have $',money,'\n')
          t(1)
          bet1 = money+1
          while bet1 &gt; money:
              bet1 = int(input('How much do you want to bet?\n'))
          fiftyfifty(bet1)
  
  </code></pre>
  
  <p>I expect it to just go through, add a tally into win or loss, then update the money. However, I am getting this error:
  <code>UnboundLocalError: local variable 'losses' referenced before assignment</code>
  If I win, it says the same thing with <code>local variable 'wins'</code>.</p>
  
  <p>As shown all variables are assigned at the top, then referenced below in subroutines. I am completely unsure on how python thinks I referenced it before assignment? </p>
  
  <p>I would appreciate any help, thank you in advance!</p>