0% found this document useful (0 votes)
6 views8 pages

Python Turtle Graphics Interface Design

This chapter discusses interface design in programming, focusing on the turtle module in Python for creating graphics. It covers concepts such as encapsulation, generalization, and the importance of clean interfaces, along with the process of refactoring and developing functions. Additionally, it emphasizes the role of documentation and debugging in ensuring effective function usage.

Uploaded by

siokengin01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Python Turtle Graphics Interface Design

This chapter discusses interface design in programming, focusing on the turtle module in Python for creating graphics. It covers concepts such as encapsulation, generalization, and the importance of clean interfaces, along with the process of refactoring and developing functions. Additionally, it emphasizes the role of documentation and debugging in ensuring effective function usage.

Uploaded by

siokengin01
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

16/08/2024

Chapter 04
Case study: interface design
CIVL1000
Computer Science

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey Adapted: Miguel Gomes da Costa Junior

Case study: interface design


• This chapter presents a case study that demonstrates a process for
designing functions that work together.
• It introduces the turtle module, which allows you to create images using
turtle graphics.
• The turtle module is included in most Python installations.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 2

1
16/08/2024

4.1 The turtle module


• Check the turtle module existence. Type in the Python interpreter:
>>> import turtle • When you run this code, it should create a new
>>> bob = [Link]() window with a small arrow that represents the
turtle. Close the window.
• Create a file named [Link] and type in the following code:
import turtle • The turtle module provides a function called
bob = [Link]() Turtle that creates a Turtle object, which we
print(bob) assign to a variable named bob.
[Link]()
• Printing bob displays something like: <[Link] object at 0xb7bfbf4c>
• 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.
• In this case there’s not much for the user to do except close the window.
Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 3

4.1 The turtle module


• Once a Turtle is created, 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: [Link](100)
• The method fd makes the turtle object bob to move forward by the distance
defined by the argument in pixels. While method bk moves backward.
• lt for left turn, and rt right turn. The argument for lt and rt is an angle in
degrees.
• The methods pu and pd stand for “pen up” and “pen down”. Each Turtle is
holding a pen, if the pen is down, the Turtle leaves a trail when it moves.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 4

2
16/08/2024

4.1 The turtle module


• To draw a right angle, add these lines to the program (after creating
bob and before calling mainloop):
[Link](100)
[Link](90)
[Link](100)
• When you run this program, you should see bob move east and then
north, leaving two-line segments behind.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 5

4.2 Simple repetition


• If you like to display ‘Hello!’ three times, you may write something like this:
print('Hello!’)
print('Hello!')
print('Hello!')
• We can do the same thing more concisely with the for statement, like this:
for i in range(3):
print('Hello!')
• You should see something like this in both ways:
Hello!
Hello!
Hello!
• This is the simplest use of the for statement; we will see more later.
Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 6

3
16/08/2024

4.2 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.
for i in range(3):
print('Hello!')

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 7

4.3 Exercises
• Let’s work with a series of exercises using the turtle module.
• Then we will discuss certain important points.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 8

4
16/08/2024

4.4 Encapsulation
• Wrapping a piece of code up in a function is called def square(t):
encapsulation. for i in range(4):
[Link](100)
• Here is the solution to put your square-drawing
[Link](90)
code into a function definition and then call the
function, passing the turtle as a parameter. square(bob)

• One of the benefits of encapsulation is that it alice = [Link]()


attaches a name to the code, which serves as a square(alice)
kind of documentation. carol = [Link]()
• Another advantage is that if you re-use the code, square(carol)
it is more concise to call a function twice than to
copy and paste the body!
Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 9

4.5 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. def square(t, length):
• However, in this version we add a length for i in range(4):
parameter to square and it can be any size. [Link](length)
[Link](90)
square(bob, 100)
• Keyword arguments include the parameter names as “keywords” in
the argument list. (not to be confused with Python keywords).
• This syntax makes the program more readable.
square(t=bob, length= 100)
• It is also a reminder about how arguments and
parameters work when you call a function, the
arguments are assigned to the parameters.
Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 10

10

5
16/08/2024

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?
• An interface is “clean” if it allows the caller to do what they want
without dealing with unnecessary details.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 11

11

4.7 Refactoring
• The process of rearranging a program to improve interfaces and
facilitate code re-use is called refactoring.
• We usually do not know enough at the beginning of a project to design
all the interfaces.
• Once you start coding, you understand the problem better. Sometimes
refactoring is a sign that you have learned something in order to
improve function interfaces and other qualities of the code.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 12

12

6
16/08/2024

4.8 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.
• This process has some drawbacks, but it can be useful if you don’t know ahead
of time how to divide the program into functions. This approach lets you design
as you go along.
Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 13

13

4.9 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):
[Link](length)
[Link](angle)
• By convention, all docstrings are triple-quoted strings, also known as
multiline strings because the triple quotes allow the string to span
more than one line.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 14

14

7
16/08/2024

4.9 docstring
• It is terse, but it contains the essential information someone would
need to use this function.
• It explains concisely what the function does (without getting into the details
of how it does it).
• It explains what effect each parameter has on the behaviour of the function
and what type each parameter should be (if it is not obvious).
• Writing this kind of documentation is an important part of interface
design.
• A well-designed interface should be simple to explain; if you have a hard time
explaining one of your functions, maybe the interface could be improved.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 15

15

4.10 Debugging
• 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.
• The requirements are called preconditions because they are supposed to be true
before the function starts executing.
• Conversely, conditions at the end of the function are postconditions that include
the intended effect of the function (like drawing line segments) and any side
effects (like moving the Turtle or making other changes).
• Preconditions are the responsibility of the caller. If the caller violates a (properly
documented!) precondition and the function doesn’t work correctly, the bug is in
the caller, not the function.
• If the preconditions are satisfied and the postconditions are not, the bug is in the
function.
• If your pre- and postconditions are clear, they can help with debugging.

Think Python: How to Think Like a Computer Scientist (2nd) by Allen B. Downey 16

16

You might also like