3 回答

TA貢獻1810條經驗 獲得超4個贊
你的直覺是對的,沒有i=i+1
循環將無限期地執行。
本質上,while
是一個啟動循環的關鍵字。編程語言中的任何循環都包含以下基本元素:
循環變量(這里,我)
循環條件或退出條件或重復直到(這里,i<=4)
在循環內執行/重復的作業/指令集
現在,如果i=i+1
不存在,則您的循環條件始終為真,因此,它將無限期地執行。因為,我們希望任務重復 5 次(i 在 0-4 的范圍內),所以i=i+1
每次循環執行這組語句時,我們需要用語句增加 i 的值。
PS:您可能想參考一些編程資源的初學者介紹。

TA貢獻1856條經驗 獲得超17個贊
i=i+1 #this is an increment operator that equals to i++ in other languages like C.
一樣,
i+= 1 #this is similar to the above.
例子,
i = 0
while i<5:
print(i)
i+=1 (or) i= i+1

TA貢獻1828條經驗 獲得超13個贊
從代碼中可以清楚地看出:
i=0 # initially i is 0
while i<=4: # while i is less than or equal 4 continue looping
t.fd(50)
t.rt(144)
i=i+1 # you increment to reach 5 at some point and stop
#otherwise, `i` will stay at 0 and therefore `i<=4` condition will always be true
沒有i=i+1代碼是這樣的:
import turtle
t=turtle.Turtle()
t.shape('turtle')
i=0
while True:
t.fd(50)
t.rt(144)
添加回答
舉報