同心圆代码会比我输入多一个圆

Concentric circles code will give me one more circle than I input

我的同心圆代码有效,但是当我输入我需要的圆数时,它会比我想要的多一个圆。例如,如果我输入 7 个圆圈,它会给我 8 个,但它只会超过 6 个圆圈。我想知道我的代码有什么问题以及为什么会这样。谢谢。

我的密码是

 if r>=50 or r<=200: #if radius is in range, draw circles
            for c in range (r, 1, int(-r/num_circles)):
                turtle.fillcolor(random.random(),random.random(),\
                random.random()) #circles will be a random color
                turtle.begin_fill()
                turtle.circle(c)
                turtle.penup()
                turtle.left(90) #concentric
                turtle.forward(r/num_circles)
                turtle.right(90)
                turtle.pendown()
                turtle.end_fill()
        turtle.hideturtle()
        time.sleep(5)
        turtle.clear() #clears screen

将您的 for 循环 for c in range (r, 1, int(-r/num_circles)) 更改为 for c in range (r, int(r/num_circles), int(-r/num_circles)) 以便它可以达到最小值而不是 1。

if r>=50 or r<=200: #if radius is in range, draw circles
    for c in range (r, int(r/num_circles), int(-r/num_circles)):
        turtle.fillcolor(random.random(),random.random(),\
        random.random()) #circles will be a random color
        turtle.begin_fill()
        turtle.circle(c)
        turtle.penup()
        turtle.left(90) #concentric
        turtle.forward(r/num_circles)
        turtle.right(90)
        turtle.pendown()
        turtle.end_fill()
        turtle.hideturtle()
    time.sleep(5)
    turtle.clear() #clears screen