您在這裡

Exercises

4 九月, 2015 - 15:33

Exercise 4.1.Download the code in this chapter from http://thinkpython. com/code/poLygon.py .

  1. Write appropriate docstrings for polygon, arc and circle.
  2. Draw a stack diagram that shows the state of the program while executing circle(bob, radius). You can do the arithmetic by hand or add printstatements to the code.
  3. The version of arcin Refactoring is not very accurate because the linear approximation of the circle is always outside the true circle. As a result, the turtle ends up a few units away from the correct destination. My solution shows a way to reduce the effect of this error. Read the code and see if it makes sense to you. If you draw a diagram, you might see how it works.

Exercise 4.2. Write an appropriately general set of functions that can draw flowers as in Figure 4.1.

Solution: http://thinkpython.com/code/flower.py,
also requires
http://thinkpython.com/code/poLygon.py.

Exercise 4.3.Write an appropriately general set of functions that can draw shapes as in Figure 4.2.

Solution: http://thinkpython.com/code/pie.py.

Exercise 4.4.The letters of the alphabet can be constructed from a moderate number of basic elements, like vertical and horizontal lines and a few curves. Design a font that can be drawn with a minimal number of basic elements and then write functions that draw letters of the alphabet.

You should write one function for each letter, with names draw_a, draw_b, etc., and put your functions in a file named letters.py. You can download a “turtle typewriter” from http://www.greenteapress.com/thinkpython/code/typewriter.py to help you test your code.

Solution: http://www.greenteapress.com/thinkpython/code/letters.py ,also requires http://thinkpython.com/ code/poLygon.py.

Exercise 4.5.Read about spirals at http://en.wikipedia.org/wiki/SpiraL ; then write a program that draws an Archimedian spiral (or one of the other kinds). Solution:http://thinkpython.com/code/spiraL.py.