listplaceloc = doornum-1
coordinatelist[listplaceloc] = turtle.pos()
coordinatelist = [1,2,3,4,5,6,7,8,9,10,11,12,13,13,14,15,16,17,18,19,20,21,22,23,24]
coordlistnum = 0    
turtle.goto(coordinatelist[coordlistnum])
coordlistnum +=1
turtle.pendown()
turtle.color("black")
while coordlistnum!= 24:
    iter (coordinatelist[coordlistnum])
    turtle.goto(coordinatelist[coordlistnum])
    coordlistnum += 1

i have tried the iter function, but it said it couldn't be iter'ed

2

There are 2 best solutions below

0
Dani On

If I understood the question correctly, you're simply trying to iterate over coordinatelist, which you're doing already. You can simply get rid of the iter (coordinatelist[coordlistnum]) because you're already iterating through the list in a while loop by implementing a counter and incrementing it.

coordlistnum = 0 
...
coordinatelist = [1,2,3,4,5,6,7,8,9,10,11,12,13,13,14,15,16,17,18,19,20,21,22,23,24]
while coordlistnum!= 24:
    turtle.goto(coordinatelist[coordlistnum])
    coordlistnum += 1

This means you'll be calling all elements of coordinatelist until you reach element number 24 (elements 1...23, because you've incremented the coordlistnum once before going into the loop and because once you increment it to 24 you'll exit the loop)

iter() returns an iterator object, which could be valid too, you'll simply have to call next() on the iterator and catch the exception like so:

coordinatelistIter = iter(coordinatelist)
try:
    while True:
        coordinates = next(coordinatelistIter)
        ...
except StopIteration:
    pass

You're probably better off with a simple for loop while slicing the list to stop at the 23rd element

for coordinate in coordinatelist[:24]:
   ...

Or enumerate and use the index and the value and break (exit the loop) once you reach the required index:

for index, value in enumerate(coordinatelistIter):
   if index > 23:
      break
   turtle.goto(value)
0
cdlane On

Since goto() requires both an X and Y coordinate, the only way I can interpret your code fragment is that you want to move:

(1, 2) -> (2, 3) -> (3, 4) -> ... -> (22, 23) -> (23, 24)

If that's the case, we do so as follows:

from turtle import Screen, Turtle

coordinatelist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

screen = Screen()

turtle = Turtle()
turtle.penup()

coordlistnum = 0

while coordlistnum < len(coordinatelist) - 1:
    turtle.goto(coordinatelist[coordlistnum:coordlistnum+2])
    turtle.pendown()
    coordlistnum += 1

screen.exitonclick()

If that's not the case, you need to further clarify your question.