'Draw Co-ordinates using Turtle

I'm trying to draw a set of co-ordinates using Python Turtle, however ,I'm stuck and have no clue how to move on.

def generate_map(x_range, y_range, locations):
    generated_map = []

    for x in range(locations):
        generated_map.append([random.randint(x_range, y_range), random.randint(x_range, y_range)])


    return generated_map

 new_generated_map = generate_map(-20,20,10)
 print("The co-ordinates are:",new_generated_map)

def print_map(speed, color, thickness, selected_map):
    print("printing map")


    turtle.speed(3)
    turtle.pencolor("black")
    turtle.pensize(4)


    for locations in (new_generated_map):
        turtle.pendown()

I know that I have to set up a loop function but I'm not sure how to write it out, still new to programming.



Solution 1:[1]

You can use turtle.setpos() or turtle.goto(), with the coordinates as the arguments:

def print_map(speed, color, thickness, selected_map):
    print("printing map")

    turtle.speed(3)
    turtle.pencolor("black")
    turtle.pensize(4)

    for locations in (new_generated_map):
        turtle.setpos(locations)
        turtle.pendown()

A few things to note:

  1. As there is no turtle.penup() in your for loop, you only need to call the turtle.pendown() method once, outside of the for loop, as it saves efficiency. If there is no turtle.penup() call in your code at all, then the turtle.pendown() can be removed entirely.
  2. There is no need to enclose the new_generated_map list in brackets in order to loop through the list.

So the improved function would be:

def print_map(speed, color, thickness, selected_map):
    print("printing map")

    turtle.speed(3)
    turtle.pencolor("black")
    turtle.pensize(4)

    for locations in new_generated_map:
        turtle.setpos(locations)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ann Zen