Home > database >  Is there a way to draw multiple circles with Turtle based on user input?
Is there a way to draw multiple circles with Turtle based on user input?

Time:01-06

I want to create something that looks like this: many circles of the same size next to each other

However, I want the number of circles to be determined by user input. I can't seem to find any information on how I might go about this.

Here's what i have so far but it does not accomplish my goals.

import turtle
 
print("How many circles?")
circnum = input()

#Summoning the turtle
t = turtle.Turtle()

#circling the circle
for i in circnum:
  r = 25
  t.circle(r)

Many thanks!

CodePudding user response:

You need to make circnum a number so that you can make a range to iterate over, and you need to move the turtle in between circles so you aren't just drawing the same circle on top of itself over and over.

import turtle
 
print("How many circles?")
circnum = int(input())

#Summoning the turtle
t = turtle.Turtle()

#circling the circle
for _ in range(circnum):
  t.circle(25)
  t.forward(5)

CodePudding user response:

I agree with @Samwise's suggestions ( 1) but if you're using standard Python 3 turtle, and not some older version or subset, I say get rid of input() and go full turtle on it:

from turtle import Screen, Turtle

RADIUS = 25
DISTANCE = 10

screen = Screen()

number_circles = screen.numinput("A Circle in a Spiral", "How many circles?", default=10, minval=1, maxval=30)

if number_circles:
    # Summoning the turtle
    turtle = Turtle()
    turtle.speed('fast')  # because I have little patience

    # Circling the circle
    for _ in range(int(number_circles)):  # numinput() returns a float
        turtle.circle(RADIUS)
        turtle.forward(DISTANCE)

    screen.exitonclick()
else:
    # user hit 'Cancel' in the number input dialog
    screen.bye()
  •  Tags:  
  • Related