简体   繁体   中英

How to get turtle to STOP after 5 circles! Python 3.x

I need my turtle to stop after 4 circles but he won't do it! Can anyone help? I wouldn't ask unless I was really stuck and I have been researching for a while.

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)

while radius == 40 - you never change radius , so it will never stop. Instead, loop over an iterable like range() :

for _ in range(5):

Remember, you don't have to use the loop variable within the loop. It's okay to use it as nothing other than a counter.

The following code would draw your N times, where you would replace N with the number of circles you want it to draw:

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--;

The reason that it drew more circles than you wanted is because you were checking against a variable that never changed.

By this I mean you were seeing if radius changed, but never changing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM