title: Unable to break a python while loop inside a function
body: <p>This program is a snakes and ladders program for my GCSE computer science (don't worry, this is just practice), and I'm having trouble breaking a while loop with a function that exists inside the loop. Here's the code:</p>
  
  <pre><code>win = False
  
  while win == False:
      print("Player 1 goes first.")
  
      def dice():
          roll = random.randint(1,6)
          return roll
  
      roll = dice()
      print("They roll a...",roll)
      player1+=roll
  
      def check():
          if player1 in ladders or player2 in ladders:
              print("A ladder has moved the player to a new position.")
          elif player1 in snakes or player2 in snakes:
              print("A snake has moved the player to a new position.")
          elif player1 &gt;= 36:
              print("Congratulations. Player 1 has won the game.")
              win = True
          elif player2 &gt;= 36:
              print("Congratulations. Player 2 has won the game.")
              win = True
  
      check()
  
      print("Player 1 is on square",player1)
  </code></pre>
  
  <p>It's obviously not finished yet, and that's not all the code. After that bit, it does the same but with player2. There's a tuple above it that the check function checks to see if the player landed on a snake or a ladder, but I haven't added the code which actually moves the player up/down the ladder/snake.</p>
  
  <p>The error is that the while loop is an infinite loop. </p>
  
  <p>I've tried changing the whole win = False or True thing to just while True and then using break where I say win = True, but then it returns an error of 'break outside loop' even though the break is obviously inside the loop. I wonder if it's because I need to return something from the function, but I'm not quite sure how to do that. Simply putting 'return win' below both win = True doesn't change anything, and the while loop still continues indefinitely. </p>
  
  <p>I've looked <a href="https://stackoverflow.com/questions/14522923/how-to-break-a-python-while-loop-from-a-function-within-the-loop">here</a> and <a href="https://stackoverflow.com/questions/16073396/breaking-while-loop-with-function">here</a> for answers but neither worked for me; I think my situation is slightly different.</p>