Think Python How to Think Like a Computer Scientist


Download 1.04 Mb.
Pdf ko'rish
bet41/190
Sana02.11.2023
Hajmi1.04 Mb.
#1740310
1   ...   37   38   39   40   41   42   43   44   ...   190
Bog'liq
thinkpython

28
Chapter 3. Functions
2. Use the previous function to draw a similar grid with four rows and four columns.
You can see my solution at thinkpython.com/code/grid.py.


Chapter 4
Case study: interface design
4.1
TurtleWorld
To accompany this book, I have written a suite of modules called Swampy. One of these modules
is TurtleWorld, which provides a set of functions for drawing lines by steering turtles around the
screen.
You can download Swampy from thinkpython.com/swampy; follow the instructions there to install
Swampy on your system.
Move into the directory that contains TurtleWorld.py, create a file named polygon.py and type
in the following code:
from TurtleWorld import *
world = TurtleWorld()
bob = Turtle()
print bob
wait_for_user()
The first line is a variation of the import statement we saw before; instead of creating a module
object, it imports the functions from the module directly, so you can access them without using dot
notation.
The next lines create a TurtleWorld assigned to world and a Turtle assigned to bob. Printing bob
yields something like:

This means that bob refers to an instance of a Turtle as defined in module TurtleWorld. In this
context, “instance” means a member of a set; this Turtle is one of the set of possible Turtles.
wait_for_user
tells TurtleWorld 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.
TurtleWorld provides several turtle-steering functions: fd and bk for forward and backward, and lt
and rt for left and right turns. Also, each Turtle is holding a pen, which is either down or up; if the



Download 1.04 Mb.

Do'stlaringiz bilan baham:
1   ...   37   38   39   40   41   42   43   44   ...   190




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling