Programming Foundations with Python >> Lessons >> Use Classes: Draw Turtles >> Quiz: Improving Code Quality
Notes on Improving Code Quality
I broke this lesson down into steps to help us produce cleaner code:
Here is the code we start with:
import turtle def draw_square(): window = turtle.Screen() window.bgcolor("red") brad = turtle.Turtle() brad.shape("turtle") brad.color("yellow") brad.speed(2) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) angie = turtle.Turtle() angie.shape("arrow") angie.color("blue") angie.circle(100) window.exitonclick() draw_square()
Problem #1
Step 1: Let’s move this repetitive series of turns into its own, new function.
Step 2: brad
the turtle will be drawing a square using this function in another part of our code. This means you will need to add a single parameter to your new function. Label it with a parameter name that would allow us to pass in brad
, or a different turtle’s name in case we tell some other turtle to make a square.
Step 2: Notice that this
brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90)
is really just this
brad.right(90) brad.forward(100)
repeated 3X. When lines of code repeat, we call it looping. Yes, it is iterating over 3X. So we need an iterator-based for loop. Remember that the directions repeat 3X.
Problem #2
Step 1:
def draw_square():
includes both code for drawing a square and a circle. Change this function name so it makes sense to include turtles that draw squares, circles, triangles, whatever.
Problem #3
Step 1: Since we summed up
brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90)
into a new function, you will need to replace the old code
brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90)
by calling the new function and passing in the turtle (the name of the turtle) who we want to make us a square.
PS. We don’t need to worry about calling any functions on “angie” the turtle because
angie.circle(100)
is like
brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90) brad.forward(100) brad.right(90)
which is basically
the_turtle_we_want_to_give_directions_to.a_specific_direction(the_parameters_needed_for_that_specific_direction)
You can scroll down to see an outline to help you further without giving away all the answers, then if you must, scroll a bit further to see the actual answer, but give it your best shot first!
Outline
def draw_square(__________): for i in range(_, _) __________.forward(100) __________.right(90) def __________(): window = turtle.Screen() window.bgcolor("red") #Below this comment we create the turtle brad who draws a square brad = turtle.Turtle() brad.shape("turtle") brad.color("yellow") brad.speed(2) #Below this comment, call our function that draws a square, and pass in the name of turtle you want to draw that square _____________(______) #Below this comment we create the turtle angie who draws a circle angie = turtle.Turtle() angie.shape("arrow") angie.color("blue") angie.circle(100) window.exitonclick() draw()
Solution
def draw_square(some_turtle): for i in range(1,5) some_turtle.forward(100) some_turtle.right(90) def draw(): window = turtle.Screen() window.bgcolor("red") #Create the turtle Brad - Draws a square brad = turtle.Turtle() brad.shape("turtle") brad.color("yellow") brad.speed(2) draw_square(brad) #Create the turtle Angie - Draws a circle angie = turtle.Turtle() angie.shape("arrow") angie.color("blue") angie.circle(100) window.exitonclick() draw_square_then_circle()