将for循环转换为while循环

时间:2014-04-22 05:50:57

标签: python-3.x

这是我的代码。任何人都可以告诉我如何将内循环更改为while循环。

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 个答案:

答案 0 :(得分:-1)

您想要的并不完全清楚,但您可以考虑使用:

while True:

然后无限循环,直到退出程序或使用:

break

Python文档的一个例子是:

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

你基本上可以把你现在拥有的东西放进循环中:

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

然而,如果没有退出或中断,这将循环连续