简体   繁体   中英

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. 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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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