Conditional loops are way to repeat something until a certain condition is met.
If the condition is never met, the loop can become infinite. In Python
conditional loops are defined with the while statement:
counter = 0
while counter < 10:
print("in the loop")
counter += 1
Use the while loop to move the turtle forward until it reaches the end of
the window.
def move_to_window_border(distance):
while turtle.xcor() + distance < turtle.window_width() / 2:
turtle.forward(distance)
Can you make the turtle move exactly to the window border?
Loops can be interrupted with the break statement. This is often used in
combination with infinite loops.
Write a while loop with a condition that is always True to draw a spiral.
Interrupt the loop when the turtle reaches a certain distance from the center.
Use the function turtle.distance(x, y) to get the turtle's distance to the
point defined by the coordinates x and y.
Note
To draw a spiral, the turtle has to rotate by a constant value and move forward by an increasing value in each repetition.
def draw_spiral(radius):
speed = 1
while True:
turtle.forward(speed)
turtle.left(10)
speed += 0.1
if turtle.distance(0, 0) > radius:
break