Chapter 4. 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, but if you are running Python using PythonAnywhere, you won’t be able to run the turtle examples (at least you couldn’t when I wrote this).
If you have already installed Python on your computer, you should be able to run the examples. Otherwise, now is a good time to install. I have posted instructions at http://tinyurl.com/thinkpython2e.
Code examples from this chapter are available from https://thinkpython.com/code/polygon.py.
The turtle Module
To check whether you have the turtle module, open the Python interpreter and type:
>>> import turtle >>> bob = turtle.Turtle()
When you run this code, it should create a new window with a small arrow that represents the turtle. Close the window.
Create a file named mypolygon.py and type in the following code:
import turtle bob = turtle.Turtle() print(bob) turtle.mainloop()
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:
<turtle.Turtle 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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access