如何让海龟停下来5圈!Python 3.x版

2024-04-19 21:15:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要我的乌龟在转了4圈后停下来,但他不肯!有人能帮忙吗?我不会问,除非我真的卡住了,我已经研究了一段时间。你知道吗

from turtle import *
import time
### Positioning
xpos= -250

ypos= -250

radius= 40

speed(10)

###Program main function
while radius == 40:
    pu()
    goto (xpos, ypos)
    pd()
    begin_fill()
    color("red")
    circle(radius)
    end_fill()
    xpos = xpos + (radius*2)

Tags: fromimporttimemainfunctionprogramfillspeed
2条回答

while radius == 40-你永远不会改变radius,所以它永远不会停止。相反,在iterable上循环,如range()

for _ in range(5):

记住,您没有在循环中使用循环变量。可以把它当作柜台用。你知道吗

下面的代码将绘制N次,将N替换为要绘制的圆数:

from turtle import *
import time
### Positioning
xpos= -250

ypos= -250

radius= 40

speed(10)

currentIterateCount = N

###Program main function
while currentIterateCount != 0:
    pu()
    goto (xpos, ypos)
    pd()
    begin_fill()
    color("red")
    circle(radius)
    end_fill()
    xpos = xpos + (radius*2)
    currentIterateCount ;

它之所以画了比你想要的更多的圆,是因为你在检查一个从未改变过的变量。你知道吗

我的意思是你看到了radius是否改变了,但从未改变过。你知道吗

相关问题 更多 >