为什么我的循环循环不能在python中工作

时间:2015-02-23 03:45:44

标签: python loops drawing turtle-graphics

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)

2 个答案:

答案 0 :(得分:2)

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,否则他们不应该在代码中寻找

答案 1 :(得分:0)

  

对于范围内的圆(num,0,-1):90(num..1)

     

In For Condition“:”是必须指示循环的开始,

     

对于范围内的圆(num,0,-1):#(num..1)

     

你的变量num也没有被定义,假设你把它放错了变量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