Joshua Schmidt
Joshua Schmidt

Reputation: 89

Stopping a turtle when it reaches a point, Python

I am currently in a beginning programming class, and I am blowing through the assignments. Right now, I have to make 3 houses with the module turtle (which I accomplished):

def drawBody(mover):
    #Rectangle part
    mover.fillcolor("blue")
    mover.begin_fill()
    for i in range(2):
        mover.forward(100)
        mover.right(90)
        mover.forward(75)
        mover.right(90)
    mover.end_fill()


    #Triangle part
    mover.fillcolor("red")
    mover.begin_fill()
    mover.left(45)
    for i in range(2):
        mover.forward(70.5)
        mover.right(90)
    mover.right(45)
    mover.forward(100)
    mover.end_fill()







#Create preproduction turtle
import turtle
wn = turtle.Screen()
josh = turtle.Turtle()


pointGoTo = -175
for houses in range(3):
    josh.penup()
    josh.goto(pointGoTo,0)
    josh.pendown()

    drawBody(josh)


    josh.right(180)
    pointGoTo = pointGoTo + 125

wn.exitonclick()

here is the while code. So I want the turtle to stop at a certain point, The top left corner of the red square. I have tried multiple points but the while just doesnt break/stop. Is my syntax off? or am I approaching this whole line of the house thing all wrong? If i am being vague, please ask what you dont understand, I really want to figure this out, but i am all out of ideas.:

def drawBody(mover):
    #Rectangle part
    mover.fillcolor("blue")
    mover.begin_fill()
    for i in range(2):
        mover.forward(100)
        mover.right(90)
        mover.forward(75)
        mover.right(90)
    mover.end_fill()


    #Triangle part
    mover.fillcolor("red")
    mover.begin_fill()
    mover.left(45)
    for i in range(2):
        mover.forward(70.5)
        mover.right(90)
    mover.right(45)
    mover.forward(100)
    mover.end_fill()

    mover.left(90)
    mover.forward(75)
    mover.left(90)
    n = mover.position()
    print(n)
    while True:
        mover.forward(100)
        n = mover.position()
        print(n)
        mover.left(90)
        mover.forward(5)
        mover.left(90)
        n = mover.position()
        print(n)
        mover.forward(100)
        mover.right(90)
        mover.forward(5)
        mover.right(90)


        if n == (-75.30,0.00):
            break







#Create preproduction turtle
import turtle
wn = turtle.Screen()
josh = turtle.Turtle()


pointGoTo = -175
for houses in range(3):
    josh.penup()
    josh.goto(pointGoTo,0)
    josh.pendown()

    drawBody(josh)


    josh.right(180)
    pointGoTo = pointGoTo + 125

wn.exitonclick()

Upvotes: 2

Views: 6356

Answers (2)

roman
roman

Reputation: 117380

first of all, you're checking in the wrong place, you ahve to check it here:

while True:
    mover.forward(100)

    mover.left(90)
    mover.forward(5)
    mover.left(90)
    n = mover.position()

    if abs(n - (-75.30, 0.00)) < 0.01:
        break

    mover.forward(100)
    mover.right(90)
    mover.forward(5)
    mover.right(90)

Your check is not succesfull because n is actually turtle.Vec2D, and coordinates are float, you can see it if you do print n[0], n[1]. There're many links on SO about comparing floats, like Testing floating point equality, for example. In your case you can do:

if abs(n - (-75.30, 0.00)) < 0.01:
    break

But I think, the best way for you would be to just paint fixed amount of times:

mover.left(90)
mover.forward(70)
mover.left(90)
for i in xrange(7):
    mover.forward(100)
    mover.left(90)
    mover.forward(5)
    mover.left(90)

    mover.forward(100)
    mover.right(90)
    mover.forward(5)
    mover.right(90)

Also you have to change your code like this:

for houses in range(3):
    josh.penup()
    josh.goto(pointGoTo,0)
    josh.pendown()

    drawBody(josh)

    pointGoTo = pointGoTo + 125

Upvotes: 1

Veedrac
Veedrac

Reputation: 60147

In here you want a stop condition.

while True:
    mover.forward(100)
    n = mover.position()
    print(n)
    mover.left(90)
    mover.forward(5)
    mover.left(90)
    n = mover.position()
    print(n)
    mover.forward(100)
    mover.right(90)
    mover.forward(5)
    mover.right(90)

You can either try and use the mover's position, or you can count the number of times it must move.

If you use the mover's position you will have to do calculations to offset from where the square starts and what direction you're going in (especially if you're at a 30° angle!), so I don't recommend that.

Instead, you can say that the square (which is blue, btw, not red) is 75px tall and you move up by 10 each iteration, so you want 7½ iterations. If you had a whole number of iterations, say 8, you could've done

for i in range(8):
    mover.forward(100)
    n = mover.position()
    ...

which counts the iterations 0..7 (there are 8 numbers from 0 to 7) and does an iteration each time. We want to break from the loop on the final, 8th iteration, which is easy because we have the break keyword and the i variable from the range keeps track of the number of iterations:

for i in range(8):
    mover.forward(100)
    n = mover.position()
    print(n)
    mover.left(90)
    mover.forward(5)
    mover.left(90)
    n = mover.position()
    print(n)

    if i == 7:
        break

    mover.forward(100)
    mover.right(90)
    mover.forward(5)
    mover.right(90)

Note that i == 7 happens on the 8th iteration, so the 8th is cancelled half-way, so 7½ iterations, moving 75 pixels upwards, are done.

Upvotes: 0

Related Questions