title: Randomly walking turtle function not doing what I want it to
body: <p>I have to write a programme where a turtle takes 90 degree turns, chosen randomly as left or right, around the screen, until it hits a wall, takes a 180 degree turn and goes back to walking around the screen. When it's hit the wall 4 times, the loop terminates. The problem I'm having is that when it bounces off the wall it just stops walking, and the loop has clearly terminated as I can close the window by clicking on it (<code>wn.exitonclick</code>). Here's the full programme:</p>
  
  <pre><code>import turtle
  import random
  
  def isInScreen(w,t):
      leftBound = w.window_width() / -2
      rightBound = w.window_width() / 2
      bottomBound = w.window_height() / -2
      topBound = w.window_height() / 2
  
      turtlex = t.xcor()
      turtley = t.ycor()
  
      stillIn = True
  
      if turtlex &lt; leftBound or turtlex &gt; rightBound or turtley &lt; bottomBound or turtley &gt; topBound:
          stillIn = False
  
      return(stillIn)
  
  def randomWalk(t,w):
      counter = 0
  
      while isInScreen(w,t) and counter &lt; 4:
          coin = random.randrange(0,2)
          if coin == 0:
              t.left(90)
          else:
              t.right(90)
          t.forward(50)
  
      t.left(180)
      t.forward(50)
      counter = counter+1
  
  wn = turtle.Screen()
  wn.bgcolor('lightcyan')
  
  steklovata = turtle.Turtle()
  steklovata.color('darkslategray')
  steklovata.shape('turtle')
  
  randomWalk(steklovata,wn)
  
  wn.exitonclick()
  </code></pre>
  
  <p>I'm confused as to why it stops, considering once the turtle bounces back, its x and y coordinates meet the requirements for isInScreen(w,t) to be true and thus go back to walking. Any ideas?<br><br>
  <strong>EDIT:</strong> Accepted Sukrit's answer as it was the easiest to relate to what I'd already programmed and gave me a few pointers on other stuff, but Brian's answer was very useful too and I'd accept both if it was possible. Thanks a lot to both of you!</p>