Chapter 4 – Case Study: Interface Design

4.1 Turtle World

A package: is a collection of modules

In this chapter, the author, Allen B. Downey wrote a package called Swampy. You can download it via

One of the modules in Swampy is TurtleWorld, which provides a set of functions for drawing lines by steering turtles around the screen.

The Swampy package is actually a friendly way to start coding. It provides a visual reward for  the lines of code we write.

Instance is a member of a set.

For instance, the TurtleWorld in this chapter is a member of the set of TurtleWorlds.

4.2 Simple repetition

There are two ways of making Bob the Turtle dance:

1st way: With repetition

In Sublime Text: “”

from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print bob

fd(bob, 100)

fd(bob, 100)

fd(bob, 100)

fd(bob, 100)

for i in range(4):
print ‘Hello!’

wait_for_user() #This function allows the program to wait for your “input” in order to proceed. If you omit to write this line of code, Python will run, execute and the close the code in a second. So, technically this function will allow you to admire your work for as long as you want.

2nd way: With a loop

from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print bob

for i in range(4):


The syntax of a for statement is similar to a function definition. It has a header that ends with a colon and an intended body. The body can contain any number of statements.

A for statement is sometimes called a loop because the flow of execution runs through the body and the loops back to the top […] Loop: A part of a program that can execute repeatedly.

Instead of repeating commands over again, the for statement help us write code in a more concise way.

4.3 Exercises

I encourage you to solve the problems from the exercises by yourself. Although, sometimes it is frustrating; at the end it is very rewarding.

Gerren Liles, a health/fitness professional said in an interview “failure is success”. This concise sentence is very compelling because I came to realize that the only walls we stumble upon are the walls we create for ourselves.

I this chapter I realized that no matter what I stumble upon I must to keep on reading and going. I stopped for almost three (3) weeks because of problem 4.3 (parts 4 and 5). My goal was to find the answer by myself without peeking at the answers. I twisted the problem over and over and I still had an error message. Needless to say that the feeling of frustration/failure was not very far.

Here is the beauty of the experience: chapter 4 provides the answers by analyzing the problems one by one. So, if I continued reading I would have realize the answers and save precious time.

The lesson is to read the whole chapter once. Make notes on the parts that I might need some help and re-read it again. The process seems a bit cumbersome. Nevertheless, it seems optimal compared to stop reading, dreading a small problem.

So, to come back to the Gerren Liles’ sentence “failure is success”. My goal now is to transform hard problems into steps for success.

Here is my work for the exercises 4.3 (parts 1 to 5):

I must say that until today the logic of exercises 4.3 (part 5) is not very intuitive. So, here is what I found with the help of a CS:

In Sublime Text:

def polygon2 (t, n, length, m):

angle = 360 / n
# t = bob
# n = n-sided polygon
# length = length of each side
# m = number of sides to be drawn
# m = m/n
for i in range (m):
fd (t, length)
lt (t, angle)

def arc (t, r, angle):
n = 50
length = (2 * math.pi * r)/n
m = (angle * n)/360
polygon2 (t, n, length, m)

4.4 Encapsulation

Wrapping a piece of code up in a function is called encapsulation. One of the benefits of encapsulation is that it attaches a name to the code, which serves as a kind of documentation. Another advantage is that if you reuse the code, it is more concise to call a function twice than to copy and paste the body! […]

Encapsulation: The process of transforming a sequence of statements into a function definition

4.5 Generalization

In Chapter 3 (part II from my notes), the concepts of parameter and argument were first introduced. Here is some information in order to understand the purpose of parameters.

Adding a parameter to a function is called generalization because it makes the function more general.

When we call the function, we replace the parameters by the arguments. As we advance, we will use and create more functions. Keeping track of “how is how” can be done using keyword argument(s).

Keyword Argument: An argument that includes the name of the parameter as a “keyword.”

For instance, we can call our function polygon like this: polygon (bob, n=5, length=100)

4.6 Interface Design

The interface of a function is a summary of how it is used: what are the parameters? What does the function do? And what is the return value? […] A description of how to use a function, including the name and descriptions of the arguments and return value […]

An interface is like a contract between a function and a caller. The caller agrees to provide certain parameters and the function agrees to do certain work.

4.7 Refactoring

[The process of] rearranging a program to improve function interfaces and facilitate code reuse is called refactoring.

4.8 A Developmental Plan

Development plan: A process for writing programs.

In this Case Study, we started by giving simple commands (forward and left) to the Turtle. Then, we created the square function. Moreover, we developed the function polygon which is a generalization of our initial square function. When we developed the circle and the arc functions. As you can see, instead of starting with the complex problem (the arc function), we the author Allen B. Downey opted for a more heuristic approach.

The book “How to Solve it” (1945 ) from the mathematician Geoge Polya describes how the heuristic approach works. Basically, you break the big problem into small pieces to solve it.

4.9 Docstring

A docstring is a string at the beginning of a function that explains the interface (“doc” is short for “documentation”) […]

The triple-quoted string, also known as a multiline string, allows the string to span more than one line.

For instance, we can use a docstring in our square function:
from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print bob

def square (t):
“””The function square draws a square with sides of the given length (100). Parameter t is the turtle.

for i in range(4):


The purpose of the docstring is to provide the essential information on the effect of the function (what the function does) and to explain the effect each parameter has on the behaviour of the function.
4.10 Debugging
Precondition: A requirement that should be satisfied by the caller before a function starts.Postcondition: A requirement that should be satisfied by the function before it ends.


Acknowledgments :

These notes represent my understanding from the book Think Python: How to Think Like a Computer Scientist written by Allen B. Downey.

Part of the chapter is transcribed and all the quotes unless specified otherwise come directly from his book.

Thank you Professor Downey for making this knowledge available.


Also, I would like to thank the open source community for their valuable contribution in making resources on programming available.

Thank you

Python lesson plan: Chapter 4

Hi there!

Here is the weekly lesson plan for the third week.

Objective :

Read chapter four (4) “Case Study: Interface Design” of the book “Think Python”. My goal is to understand and link the new terminology.

Readings :

Think Python

How to Think Like a Computer Scientist

by Allen B. Downey

Chapter 4
Case Study: Interface Design

Incorporation :

The format of this chapter is a case study. So, it is time to play around with all the concepts we have seen so far.

Attending meetup meetings about Python and hang out with people who love to code has been very useful.

Assessment :

Exercises: 4.1 – 4.4