converting for loop to while loop

66 Views Asked by At

here is my code. can anybody tell me how to change the inner loop to a while loop.

import turtle 
import time   
wn= turtle.Screen() 
alex= turtle.Turtle() 
alex.hideturtle() 
alex.pensize(5) 
list= [['alex.left(90)','alex.forward(200)','time.sleep(1)'], 
['alex.right(90)','alex.forward(75)','time.sleep(1)'], 
['alex.right(90)','alex.forward(55)','time.sleep(1)'], 
['alex.penup()','alex.goto(65,136)','alex.pendown()','alex.circle(10)','time.sleep(1)'], 
['alex.penup()','alex.goto(75,127)','alex.pendown()','alex.goto(75,98)','time.sleep(1)'], 
['alex.right(90)','alex.forward(30)','time.sleep(1)','alex.goto(100,98)','time.sleep(1)'], 
['alex.penup()','alex.goto(75,98)'], 
['alex.pendown()','alex.left(90)','alex.forward(45)','time.sleep(1)'] 
,['alex.right(120)','alex.right(180)','alex.forward(50)','time.sleep(1)'], 
['alex.penup()','alex.goto(74.00,51)','alex.pendown()','alex.right(120)','alex.forward(45)','alex.left(120)']] 
for items in list:
  for sublist in items:
     exec(sublist) 
  wn.exitonclick() 
1

There are 1 best solutions below

2
Maximas On

It's not entirely clear what you want, however you could consider using the :

while True:

It would then infinitely loop until you either exit the program, or use:

break

An example from the Python docs is:

while True:
    n = raw_input("Please enter 'hello':")
    if n.strip() == 'hello':
        break

You could essentially put what you currently have into the loop:

while True:
    for items in list:
        for sublist in items:
            exec(sublist) 
        wn.exitonclick() 

However without the exit, or break, this would loop continuously.