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)
添加回答
举报
