Usually you would describe a circle using the radius instead of the diameter, so now I’d like you to define a function that allows you to provide the radius. The function will also need the name of a turtle to draw with, allowing this function to be used on any turtle your program might have.
def draw_circle(t_name, r):
t_name.dot(r*2)
This function has a longer name, but it’s clear what it will do. When I call the function, it changes the state of the world by drawing a circle on the screen, with the given turtle.
from turtle import Turtle
tina = Turtle()
def draw_circle(t_name, r):
t_name.dot(r*2)
draw_circle(tina, 50)
I want to be able to draw colourful circles too, so I’ll add a parameter for the colour. The turtle recognises lots of strings as colour names, such as "red", "green", and "violet". Parameters can be strings, so you’ll need to add a parameter for the colour name and pass colour names as strings.
def draw_circle(t_name, r, col):
t_name.color(col)
t_name.dot(r*2)
The turtle library uses the US spelling of ‘colour’ for the color function, which sets the colour of the turtle.
Now you can create coloured circles.
draw_circle(tina, 150, "blue")
draw_circle(tina, 100, "red")
draw_circle(tina, 50, "yellow")