The goal of this lab and project is to incorporate loops and conditionals into your code as well as provide more practice in encapsulating code and concepts for later re-use and manipulation.
import turtle
import random
import sys
print sys.argv
Save your file, cd to your working directory, and then run your lab3.py file. What do you see?
Now type some additional things on the command line after python lab3.py. For example, try:
$ python lab3.py hello world 1 2 3
What do you see?
The sys package gives us the ability to see what the user has typed on the command line. Each individual string (defined by spaces) from the command line is an entry in a list, which is a data type that we describe as a sequential container.
print sys.argv[0] print sys.argv[3] * 3 print int( sys.argv[3] ) * 3
Then run the program using the following command.
$ python lab3.py three times 3
What is going on? The first of the above three lines prints out the first item in the sys.argv list. The second line accesses the fourth item in the sys.argv list, which is a string with the digit '3' in it, and multiplies it by 3, which repeats the string three times. The third line accesses the fourth item in the sys.argv list, converts it to an integer type and then multiplies it by 3, which prints out the results of 3*3.
How could you use the capability to access values from the command line in a program?
def star(x, y, size): '''Draws a 5-pointed star starting at location (x,y) with sides with the given size.''' turtle.up() turtle.goto(x, y) turtle.down() for i in range(5): turtle.forward(size) turtle.left(144) turtle.tracer(False) turtle.color(1.0, 1.0, 0.2) n = int(sys.argv[1]) for i in range(n): star(random.randint(-300, 200), random.randint(0, 200), random.randint(5, 15)) turtle.update() raw_input('Enter')
Run the code using:
$ python lab3.py 50
Try running it with other command line values and see what happens. Try running it with no command line value.
For example, are there reasonable bounds on the number of stars in the image? What if the user does not put a command line parameter?
One strategy is to pick a default value (e.g. 100) and create a variable to hold the default value.
n = 100
If the user gives you a new value, then use the new value.
# check for user input if len(sys.argv) > 1: n = int(sys.argv[1])
The idea of having default behavior that the user can override is common in computer programs.
$ python >>> range(10045)
Try it again with other arguments. (I recommend using smaller integers.) What does the function return?
Try giving the function two arguments. What does it return? Try several different pairs of arguments.
Try giving the function three arguments. What does it return? Try several different sets of arguments.
More information on the range function is stored in its docstring. You can access that by typing:
>>> help(range)
(You can exit the help by pressing the q-key.) How does it match with what you discovered?
Inside the loop, print out the loop variable. The loop variable is the symbol after the for keyword and is also called the loop control variable. The code below is an example.
def star2(rays, size): '''Draws a star with the given number of rays, each of length the given size.''' # loop over the list returned by the range function for i in range(rays): print i
Put a call to the star2 function with the arguments 10 and 50 in your main code section. Run your program and see what prints out.
def star2(rays, size): '''Draws a star with the given number of rays, each of length the given size.''' # loop over the list returned by the range function for i in range(rays): turtle.setheading(i * 360 / rays) turtle.forward(size) turtle.backward(size)
What is this going to do? What happens if you give it different arguments?
When you have it working correctly, you should be able to control the number of lines and the length of the lines on the command line.
Put all of your top-level code in the body of a new function called "main". After the main function definition, put the following top-level code.
if __name__ == "__main__": main()
Run your file.
The above conditional statement will be true only when you run the python file on the command line. If you were to import this file into another python program, the conditional statement would evaluate to false and the main function would not run.
The real benefit is that you can write test functions for a collection of functions--like your shapes.py file--without having your test function interfere with importing it into other programs.
From now on, your files should always have that conditional in front of top-level code.