# Solutions 2.5: Turtle Circles and Arcs

Here are the solution to the exercises in Lesson 2.5: Turtle Circles and Arcs.

### Exercise 1

Draw a 7-color rainbow using Python Turtle Graphics.

#### Solution

Rainbow is one of our favorite arcs. It is always so pretty with beautiful colors. To draw a simple rainbow, we need seven colors – red, orange, yellow, green, blue, indigo, and violet. We first draw the red arc. To draw the orange arc, we need to make the radius smaller by the line width and also move the starting point by line width. We repeat the same thing for the remaining arcs. We also have to make sure that the starting direction of each arc is North.

Here is a program that draws a simple seven-color rainbow. You can store it in file rainbow.py in folder Unit2.

``````# Get ready to draw
from turtle import *
width = 30
pensize(width+1)
# Draw red half-circle
penup()
pendown()
pencolor('red')
# Draw orange half-circle
penup()
pendown()
pencolor('orange')
# Draw yellow half-circle
penup()
pendown()
pencolor('yellow')
# Draw green half-circle
penup()
pendown()
pencolor('green')
# Draw blue half-circle
penup()
pendown()
pencolor('blue')
# Draw indigo half-circle
penup()
pendown()
pencolor('indigo')
# Draw violet half-circle
penup()
pendown()
pencolor('violet')
# Finish
hideturtle()``````

As you can see, the code is fairly straightforward. You may have noticed that we use a new type of statement, the subtraction-assignment statement: radius -= width. As the name suggests, it combines subtraction and assignment into one statement. It is equivalent to radius = radius – width and can be read as “reduce radius by width“.

We also used a little trick to make the drawing nicer. Even though we make radius smaller by 30 pixels, we draw the arcs 31 pixels wide. This way, the arcs very sightly overlap and there is no white space between the arcs! Experiment with changing the pen width and the radius reduction and see what happens!

Feel free to just copy-and-paste the code above. And don’t forget to modify it and experiment with different values!

Here is the result of running rainbow.py – a beautiful seven-color rainbow:

#### Improving Rainbow Code

So, how did you like the rainbow code? Did you notice that we keep doing the same thing over and over? And that the code between the comments is nearly identical with the only difference being the color of turtle pen? Remember, good programmers are “lazy” – they don’t write more code than absolutely necessary. And this is way too much code!

So, let us borrow a few concepts from Unit 3: Functions and Conditionals and show you how to make the code shorter, easier to read, and easier to maintain.

##### Python Functions

Similarly to other high-level programming languages, Python supports user-defined functions. Functions allow us to give a name to a block of code and then call that block of code over and over again using just that name. Remember that, in Python, block of code is just a bunch of indented statements.

To make the rainbow code better, we first define a (user-defined) function draw_arc() which takes color as its parameter and draws one arc of the rainbow. And then, instead of calling the same set of statements seven times, we only call the function seven times. Isn’t it so much easier to read?

##### Code Maintenance

Now that we wrote such beautiful code, let us talk about how to maintain it. Imagine that, after a while, you are not happy with the way the rainbow looks. Perhaps it looks too round and you want to make the rainbow more like a rectangle. If you use rainbow2.py as your starting point, you only need to replace circle(radius, 180) with drawing a partial rectangle in one place. However, if you use rainbow.py as your starting point, you need to do the replacement 7 times! This is why we say that rainbow2.py is easier to maintain than rainbow.py.

### Exercise 2

Modify the code from Exercise 1 to draw a square rainbow.

#### Solution

This exercise is one of the reasons why we have improved the code in Exercise 1 and used a function instead of repeating the same code seven times. We really did not want to make bad code even worse 🙂

To make a square rainbow, we first save rainbow2.py into a file named rainbow_square.py. Use File > Save As… in the IDLE editor window to do this. To make the rainbow square, we need to replace half-circles with half-squares. The height of each half-square is the same as the radius of the half-circle and the width of the half-square is the same as the diameter of the half-circle. Everything else remains the same as before.

See how easy this was? Imagine doing this with the original code in rainbow.py. You would have to do this change seven times! This is why we say that rainbow2.py is easier to maintain then rainbow.py.

And here is the square rainbow: