Exercise -1

Write a program that prints n lines with n repetitions of a character. For example

repeat_character(character='*', n=5)

*
**
***
****
*****

Exercise 0

In this chapter we wrote a function called polygon:


In [47]:
from swampy.TurtleWorld import *

def polygon(t, n, length):
    angle = 360.0 / n
    for i in range(n):
        fd(t, length)
        lt(t, angle)
        
if __name__ == '__main__':
    world = TurtleWorld()
    bob = Turtle()
    bob.delay = 0.01
    polygon(bob, n=5, length=50)
    wait_for_user()

Let's modify this so that we pass in the arguments to polygon from the command line. To do this we will use the argv function from the sys module:


sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string. https://docs.python.org/2/library/sys.html#sys.argv


What we want to be able to do is call

$ ./draw_polygon 5 50

and this will pass in 5 as the number of sides of the polygon and 50 the length of each side. (Note that in this case I have used the hashbang #!/usr/bin/env python and chmod u+x.)

Exercise 4.1.1

Write appropriate docstrings for polygon, arc and circle.

Exercise 4.1.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 print statements to the code.

Exercise 4.2

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

Exercise 4.3

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


In [ ]:


In [ ]: