为什么我的循环在 python 中不起作用

why won't my circle loop work in python

import turtle
import time
import random

n = int(input("how many circles do you want? "))
radius = int(input("Radius?"))

turtle.forward(radius)
turtle.left(90)

for circle in range(num, 0, -1):90 (num..1)
    turtle.begin_fill()
    turtle.color(random.random(),random.random(), random.random())  
    turtle.circle(radius * circle / num)
    turtle.end_fill()

    turtle.left(90)
    turtle.forward(radius / num)
    turtle.right(90)
for circle in range(num, 0, -1):90 (num..1)

那是无效的 Python 语法。假设它是一个评论,它将是:

for circle in range(num, 0, -1):     # num..1

但是,如果您记住这一点,您会发现自己是一个更好的艺术实践者:代码告诉您如何事情是如何完成的,评论告诉您为什么他们完成了。

任何查看 Python 代码的人都应该已经意识到循环从 num 开始倒计时(顺便说一下,它应该是 n,或者顶部的输入应该分配给num) 到 1,否则他们不应该 代码。

For circle in range(num, 0, -1):90 (num..1)

In For Condition “:” is a must to Indicate the start of the loop,

For circle in range(num, 0, -1): #(num..1)

Your variable num is not defined as well, Assuming you have misplaced it with variable n.

这会给你同心圆。您可以根据用户输入循环它。将整个事情包装成一个 for 循环以简化编码,然后将其放入一个函数中。

import turtle as tu

# initial radius
radius = 100
# distance between circles
distance = 30

# pen up
tu.up()
# move pen to point x, y
# keeps the center of the circle at canvas center
tu.goto(0, -radius)
# pen down
tu.down()
tu.circle(radius)
# increase the radius value by distance
radius  += distance
# pen up
tu.up()
# move pen to point x, y
# keeps the center of the circle at canvas center
tu.goto(0, -radius)
# pen down
tu.down()
tu.circle(radius)
tu.done() #done