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:
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.)
In [ ]:
In [ ]: