4.2.4 Dartboard
A variable to control the radius of each circle and a mathematical expression to increase the radius on each iteration.
a) Complete the `for` loop to draw the circles.
b) Only draw circles with a radius greater than 50.
c) Modify the radius to be a random number each time a circle is drawn.
d) Ensure the turtle draws faster for better performance.
To create a program that draws circles on a dartboard using the turtle graphics library in Python, we can break it down into steps outlined in your question. Below is the complete code with explanations for each part.
import turtle
import random
# Set up the turtle
turtle.speed(0) # Ensure the turtle draws faster
turtle.penup() # Don't draw while moving to the first position
turtle.goto(0, -50) # Start drawing from a specific position
turtle.pendown()
# a) Complete the `for` loop to draw the circles.
for i in range(10): # You can adjust the range for more or fewer circles
radius = random.randint(51, 100) # c) Modify the radius to be a random number each time a circle is drawn.
# b) Only draw circles with a radius greater than 50
if radius > 50:
turtle.circle(radius) # Draw the circle with the random radius
turtle.penup()
turtle.goto(0, -radius - (i + 1) * 10) # Move down for the next circle
turtle.pendown()
# Finish up
turtle.hideturtle() # Hide the turtle cursor
turtle.done() # Finish the drawing
### Explanation:
– Turtle Speed: Setting `turtle.speed(0)` makes the