2014-04-22 237 views
0

這裏是我的代碼。任何人都可以告訴我如何將內部循環改爲while循環。將循環轉換爲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() 
+2

你能告訴我們爲什麼你想這樣做嗎? –

+0

'for'循環處理數組中的每個元素。這是一個瘋狂的猜測,OP只想處理一些元素。那是你打算做的@ user3558932? – alvits

回答

-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() 

但是沒有退出,或打破,這將循環不斷

+0

這只是將當前代碼封裝在'while True'中,並沒有真正回答這個問題。 –

+0

解釋並不十分清楚,我假設他們只是想要內循環(假設它是for循環)不斷循環......我沒有看到任何轉換它的時間 – Maximas