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

时间:2019-01-20 05:17:43

标签: python turtle-graphics

我的同心圆代码有效,但是当我输入所需的圆数时,它会比我想要的多出一个圆。例如,如果我输入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

1 个答案:

答案 0 :(得分:0)

将您的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