Interface Design
Interface Design
Interface design
Qëndrim Krasniqi
The turtle module
To check whether you have the turtle module, open the Python
interpreter and type
When you run this code, it should create a new window with small
arrow that represents the turtle. Close the window.
Qëndrim Krasniqi
The turtle module
Create a file named mypolygon.py and type in the following code:
mypolygon.py
import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()
When you run this code, it should create a new window with small
arrow that represents the turtle. Close the window.
Qëndrim Krasniqi
The turtle module
The turtle module (with a lowercase ’t’) provides a function called
Turtle (with an uppercase ’T’) that creates a Turtle object, which we
assign to a variable named bob. Printing bob displays something like:
>>> print(bob)
<turtle.Turtle object at 0x0000028FC2DEECC0>
This means that bob refers to an object with type Turtle as defined
in module turtle.
mainloop tells the window to wait for the user to do something,
although in this case there’s not much for the user to do except close
the window.
Qëndrim Krasniqi
The turtle module
Once you create a Turtle, you can call a method to move it around the
window. A method is similar to a function, but it uses slightly different
syntax. For example, to move the turtle forward:
mypolygon.py
import turtle
bob = turtle.Turtle()
bob.fd(100)
turtle.mainloop()
Qëndrim Krasniqi
The turtle module
The method, fd, is associated with the turtle object we’re calling bob.
Calling a method is like making a request: you are asking bob to move
forward.
The argument of fd is a distance in pixels, so the actual size depends
on your display.
Qëndrim Krasniqi
The turtle module
The following code draws a right angle:
mypolygon.py
import turtle
bob = turtle.Turtle()
bob.fd(100)
bob.lt(90)
bob.fd(100)
turtle.mainloop()
Qëndrim Krasniqi
1.1 Exercises
1. Create a file named downstairs.py and write a program that draws the
following figure.
Qëndrim Krasniqi
1.1 Solutions
downstairs.py
import turtle
uc = turtle.Turtle()
uc.fd(50)
uc.rt(90)
uc.fd(50)
uc.lt(90)
uc.fd(50)
uc.rt(90)
uc.fd(50)
uc.lt(90)
uc.fd(50)
uc.rt(90)
uc.fd(50)
uc.lt(90)
turtle.mainloop()
Qëndrim Krasniqi
1.2 Exercises
1. Create a file named upstairs.py and write a program that draws the
following figure.
Qëndrim Krasniqi
The turtle module
Also, each Turtle is holding a pen, which is either down or up. If the pen
is down, the Turtle leaves a trail when it moves. If the pen is up, the
Turtle doesn’t leave a trail when it moves.
Turtle object has two extra methods:
• pu, which stands for pen up. If you call this method on turtle, the
turtle doesn’t draw anything while walking.
• pd, which stands for pen down. If you call this method on turtle, the
turtle draws lines while walking (pd is the default).
Qëndrim Krasniqi
2.1 Exercises
1. Create a file named horizontally.py and write a program that draws the
following figure.
Qëndrim Krasniqi
2.1 Solutions
horizontally.py
import turtle
dudije = turtle.Turtle()
dudije.fd(100)
dudije.lt(90)
dudije.pu()
dudije.fd(100)
dudije.lt(90)
dudije.pd()
dudije.fd(100)
turtle.mainloop()
Qëndrim Krasniqi
2.2 Exercises
1. Create a file named vertically.py and write a program that draws the
following figure.
Qëndrim Krasniqi
Simple repetition
Chances are you wrote something like this (for mypolygon.py):
mypolygon.py
import turtle
bob = turtle.Turtle()
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
mypolygon.py
import turtle
bob = turtle.Turtle()
for i in range(4):
bob.fd(100)
bob.lt(90)
turtle.mainloop()
Qëndrim Krasniqi
Simple repetition
The syntax of a for statement is similar to a function definition. It has
a header that ends with a colon and an indented body. The body can
contain any number of statements.
A for statement is also called a loop because the flow of execution
runs through the body and then loops back to the top. In this case, it
runs the body four times .
Qëndrim Krasniqi
3.1 Exercises
1. Make downstairs.py code more concise using a for statement.
Qëndrim Krasniqi
3.1 Solutions
downstairs.py
import turtle
uc = turtle.Turtle()
for i in range(3):
uc.fd(50)
uc.rt(90)
uc.fd(50)
uc.lt(90)
turtle.mainloop()
Qëndrim Krasniqi
3.2 Exercises
1. Make upstairs.py code more concise using a for statement.
Qëndrim Krasniqi
4.1 Exercises
The following is a series of exercises using TurtleWorld. They are meant
to be fun, but they have a point, too. While you are working on them,
think about what the point is.
Write a function call that passes bob as an argument to square, and then
run the program again.
Qëndrim Krasniqi
4.1 Exercises
2. Add another parameter, named length, to square. Modify the body so
length of the sides is length, and then modify the function call to provide
a second argument. Run the program again. Test your program with a range
of values for length.
3. Make a copy of square and change the name to polygon. Add another
parameter named n and modify the body so it draws an n-sided regular
polygon. Hint: The exterior angles of an n-sided regular polygon are 360/n
degrees.
Qëndrim Krasniqi
Encapsulation
The first exercise asks you to put your square-drawing code into a
function definition and then call the function, passing the turtle as a
parameter. Here is a solution:
def square(t):
for i in range(4):
t.fd(100)
t.lt(90)
square(bob)
Qëndrim Krasniqi
Encapsulation
The innermost statements, fd and lt are indented twice to show that they are
inside the for loop, which is inside the function definition. The next line,
square(bob), is flush with the left margin, which indicates the end of both the
for loop and the function definition.
Inside the function, t refers to the same turtle bob, so t.lt(90) has the same
effect as bob.lt(90). In that case, why not call the parameter bob? The idea is
that t can be any turtle, not just bob, so you could create a second turtle and pass
it as an argument to square:
alice = turtle.Turtle()
square(alice)
Qëndrim Krasniqi
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 re-use the code, it is more concise to call a function
twice than to copy and paste the body!
Qëndrim Krasniqi
Generalization
The next step is to add a length parameter to square. Here is a
solution:
square(bob, 100)
Qëndrim Krasniqi
Generalization
Adding a parameter to a function is called generalization because it
makes the function more general: in the previous version, the square is
always the same size; in this version it can be any size.
Qëndrim Krasniqi
Generalization
The next step is also a generalization. Instead of drawing squares,
polygon draws regular polygons with any number of sides. Here is a
solution:
def polygon(t, n, length):
angle = 360 / n
for i in range(n):
t.fd(length)
t.lt(angle)
polygon(bob, 7, 70)
Qëndrim Krasniqi
Generalization
These are called keyword arguments because they include the
parameter names as “keywords” (not to be confused with Python
keywords like while and def).
This syntax makes the program more readable. It is also a reminder
about how arguments and parameters work: when you call a function,
the arguments are assigned to the parameters.
Qëndrim Krasniqi
A development plan
A development plan is a process for writing programs. The process we
used in this case study is “encapsulation and generalization”. The steps
of this process are:
1. Start by writing a small program with no function definitions.
2. Once you get the program working, identify a coherent piece of it,
encapsulate the piece in a function and give it a name.
3. Generalize the function by adding appropriate parameters.
4. Repeat steps 1–3 until you have a set of working functions. Copy and paste
working code to avoid retyping (and re-debugging).
5. Look for opportunities to improve the program by refactoring. For example,
if you have similar code in several places, consider factoring it into an
appropriately general function.
Qëndrim Krasniqi
docstring
A docstring is a string at the beginning of a function that explains the
interface (“doc” is short for “documentation”). Here is an example:
def polyline(t, n, length, angle):
"""Draws n line segments with the given length and
angle (in degrees) between them. t is a turtle.
"""
for i in range(n):
t.fd(length)
t.lt(angle)
Qëndrim Krasniqi