✐

✐

“91974˙CH01˙ﬁnal” — 2012/12/14 — 13:55 — page 42 — #42

✐

✐

✐

✐

✐

✐

42 CHAPTER 1 Introduction

Exercises

1.42 Modify the drawCircle function so that the circle is drawn with the center at the

turtle’s present position.

1.43 The drawCircle function is somewhat ineﬃcient: for small circles, 360 sides is really

overkill and for very large circles 360 sides might be too few. See if you can devise a

way to make the number of sides and turning angle dependent on the radius so that

smaller circles use fewer sides and larger circles use more.

1.6 Summary

This chapter introduced the following fundamental building blocks of programming and

Python:

Primitive types

Expressions

Naming objects

Using modules and functions provided by Python

Writing your own functions to extend the functionality provided by Python

In addition, the approach we followed to use the turtle to draw a circle illustrates an

important problem-solving pattern that you will use many times as you progress through

this book. That pattern can be summarized as follows:

Simplify in order to understand the problem better.

Generalize to solve many problems with one function.

Build on what you have learned to solve more complex problems.

In subsequent chapters we will continue to use these basic building blocks. There will be

more tools to add to your toolbox. We have glossed over a few details on some of the

ideas introduced in this chapter but will return to them later. Keep in mind the idea

behind this book: to focus on problem solving while continually adding to your knowledge

of programming and computer science.

✐

✐

“91974˙CH01˙ﬁnal” — 2012/12/14 — 13:55 — page 43 — #43

✐

✐

✐

✐

✐

✐

Bibliography 43

Key Terms

abstraction dot operator loop variable Python

algorithm expression module repetition

assignment statement ﬂoating-point number name representation

block generalization object sequence object

complex number instance parameter simpliﬁcation

computer science integer program type

constructor keywords programming language variable

dereferencing list prompt

Python Keywords

def from in

for import range

Bibliography

[BES05] Chris L. Barrett, Stephen G. Eubank, and James P. Smith. If smallpox strikes

portland. Scientiﬁc American, March 2005.

[Eco06] Economist. The physical internet. The Economist, June 2006.

[fold] Stanford folding@home project. http://folding.stanford.edu

[How97] Kenneth R. Howard. Unjamming traﬃc with computers. Scientiﬁc American,

1997.

[Zei99] Paul Zeitz. The Art and Craft of Problem Solving. Wiley, 1999.

✐

✐

“91974˙CH01˙ﬁnal” — 2012/12/14 — 13:55 — page 44 — #44

✐

✐

✐

✐

✐

✐

44 CHAPTER 1 Introduction

Programming Exercises

1.1 Using the drawSquare function, you can have the turtle draw an interesting ﬂowerlike

shape by drawing many squares. Each square is drawn after turning the turtle by

some number of degrees between each square. Write a function drawflower that

takes the number of squares to draw as a parameter (numSquares) and draws a ﬂower

by repeating the square numSquares times. You will need to ﬁgure out how far to

turn the turtle based on numSquares.

1.2 Write a function to make the turtle draw a ﬁve-pointed star.

1.3 Write a function to make the turtle draw an n-pointed star when n is restricted to

be odd.

1.4 Write a function to have the turtle draw a simple line drawing of anything you want.

1.5 There are two more functions that the turtle understands: begin fill() and end fill().

When begin

fill is called, the turtle keeps track of its starting point, and all the

lines it has drawn until end

fill is called. When end fill is called, the turtle ﬁlls

in the space enclosed by the lines that the turtle has drawn. Use these new functions

to draw a more interesting picture.

Start Free Trial

No credit card required