vendredi 10 avril 2020

Iterating through a list of coordinates?

I have two functions where the first generates a random list of coordinates. The second function, I need it to iterate through the list of coordinates and draw each coordinate. When I execute my code, I get an error and not sure why.

Here is my code:

import random
import turtle

def generate_map(x_range, y_range, locations):

  generated_map = []
  for x in range(locations):
    random_x_points = random.randint(-x_range,x_range)
    random_y_points = random.randint(-y_range,y_range)
    generated_map.append([random_x_points, random_y_points])
  return generated_map

copied_map = generate_map(300,300,10)
print("Map Points are:", copied_map)

def print_map(speed, color, thickness, selected_map):
  print("printing map")
  turtle.penup()
  turtle.setpos(selected_map[0][0])
  turtle.pendown()
  for x in range(len(selected_map)):
        turtle.speed(speed)
        turtle.pencolor(color)
        turtle.pensize(thickness)
        turtle.goto(selected_map[x])
  turtle.setpos(selected_map[0][0])

print_map(5,"green",5,copied_map)

I think the error could be because of this:

turtle.goto(selected_map[x]) 

it says it is not iterable but I did this before in a previous exercise and it worked perfectly so I don't know where its going wrong and what to do to fix it? This is the way to access elements in a list so I'm not sure why it isn't working unless I'm not accessing the elements from the list correctly or should I use a nested list?




Aucun commentaire:

Enregistrer un commentaire