In python, how can I count the times a condition is met within a function?
<p>I am very new to programming in general, and while I'm sure this looks like homework, it likely is for somebody, but I'm teaching myself, so it's "self-homework"?</p>

<p>Anyway, I want to count the number of times a turtle leaves the window as it randomly makes squares. I also wanted to put a dot down at every point it exits the screen, but that's just for my own fun.</p>

<p>I know I am setting outs to 0 every time through, but I can't figure out how to create an accumulator pattern (if that's the right thing to do) within a function like this that already has to return a value.</p>

<p>Here's my code:</p>

<pre><code>import random
import turtle

def isInScreen(w,t):

    leftBound = - w.window_width()/2
    rightBound = w.window_width()/2
    topBound = w.window_height()/2
    bottomBound = -w.window_height()/2

    turtleX = t.xcor()
    turtleY = t.ycor()


    stillIn = True
    outs = 0

    if turtleX &gt; rightBound or turtleX &lt; leftBound:
        t.dot()
        t.right(180)
        t.forward(50)
        outs += 1
        print(outs)
        return outs

    if turtleY &gt; topBound or turtleY &lt; bottomBound:
        t.dot()
        t.right(180)
        t.forward(50)
        outs += 1
        print(outs)
        return outs

    if outs == 4:
        stillIn = False

    return stillIn

t = turtle.Turtle()
wn = turtle.Screen()

t.shape('turtle')
while isInScreen(wn,t):
    coin = random.randrange(0,2)
    if coin == 0:
        t.left(90)
    else:
        t.right(90)

    t.forward(50)

wn.exitonclick()
</code></pre>

<p>Any advice would be appreciated.</p>
