Search code examples
pythonpython-turtle

python cycle through list and stop at a random point


I am trying to make the turtle move in a circle and stop randomly at one point, below is what I have, but i am not sure how to make it stop at a point randomly. I tried using random.choice, but that makes the turtle break the circle and move to the point but I want it to move in a continuous circle and stop at one point. The coordinates are in order to draw a circle

coordinates = ((20,-125),(50,-115),(80,-95),(100,-75),(120,-50),(130,-20),(125,20),(115,45),(100,75),(75,100),(45,115),(20,120),(-15,120),(-45,115),(-70,100),(-95,70),(-115,50),(-125,20),(-125,-15),(-115,-50),(-100,-75),(-75,-100),(-45,-115),(-10,-125))

for i in coordinates:
    pointer.goto(i)


Solution

  • I understand that you want to execute each one of the elements on the tuple in order and stop at a random point. You could have a random integer smaller than the length of the list and stop at that point, something like this:

    import random
    
    coordinates = ((20,-125),(50,-115),(80,-95),(100,-75),(120,-50),(130,-20),(125,20),(115,45),(100,75),(75,100),(45,115),(20,120),(-15,120),(-45,115),(-70,100),(-95,70),(-115,50),(-125,20),(-125,-15),(-115,-50),(-100,-75),(-75,-100),(-45,-115),(-10,-125))
    
    stop_point = random.randrange(len(coordinates))
    
    for i in coordinates[:stop_point]:
        pointer.goto(i)