Search code examples
positionturtle-graphics

How to stop turtle when in position -200, 0


How to stop turtle at -200,0 When i set it to = -200, 0 it says invalid syntax

t.speed(20)
t.goto(-200, 0)
t.color('red')
t.fillcolor('yellow')
t.begin_fill()
t.width(3)
while True: 
    t.fd(400)
    t.lt(170)
    if abs(t.pos()) < -201:
        t.end_fill()
        break
t.mainloop()              


Solution

  • This test has some problems:

    if abs(t.pos()) < -201:
    

    The abs() of any number can't be less than a negative number. Also, pos() doesn't return a number, it returns a tuple. I'm guessing what you really want is:

    if t.distance((-200, 0)) < 1:  # turtle positions are float, not exact
    

    Complete solution:

    import turtle
    
    turtle.speed('fastest')
    turtle.setx(-200)
    turtle.width(3)
    turtle.color('red', 'yellow')
    
    turtle.begin_fill()
    
    while True:
        turtle.forward(400)
        turtle.left(170)
    
        if turtle.distance((-200, 0)) < 1:
            break
    
    turtle.end_fill()
    
    turtle.hideturtle()
    turtle.mainloop()
    

    Output:

    enter image description here

    Note that fill pattern is system dependent in this case due to lines crossing.