简体   繁体   English

为什么我的Circle Loop无法在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)

That is not valid Python syntax. 那是无效的Python语法。 Assuming it's meant to be a comment, it would be: 假设它应该是评论,则为:

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

However, you'll find yourself a better practitioner of the art if you remember this: the code tells you how things are done, comments tell you why they are done. 但是,如果你还记得这个,你会发现自己的艺术从业者更好:代码告诉你如何做事,意见告诉你为什么他们这样做。

Anyone looking at Python code should already realise that loop counts down from num (which should possibly be n by the way, or the input at the top should assign to num ) to 1 , else they shouldn't be looking at the code. 任何查看Python代码的人都应该已经意识到,循环从num递减计数(顺便说一句,它应该为n ,或者顶部的输入应该分配给num )为1 ,否则他们就不必看代码了。

For circle in range(num, 0, -1):90 (num..1) 对于范围为(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) 对于范围为(num,0,-1)的圆:#(num..1)

Your variable num is not defined as well, Assuming you have misplaced it with variable n. 您的变量num也没有定义,假设您将其与变量n放在一起了。

This will give you concentric circles. 这会给你同心圆。 You can loop it according to user inputs. 您可以根据用户输入进行循环。 Wrap the whole thing into a for loop to streamline coding and then into a function. 将整个内容包装到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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