Lab Exercise 3: Loops, conditionals and command line parameters
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.
- In your personal file space, make a folder called project3.
- Open a text editor (e.g TextWrangler). Create a new file called lab3.py. Put a comment at the top of the file with your name, date, and the file name. Save it in the project3 folder.
After the comment lines, write the command to tell python to import the
turtle package and the sys package:
First, let's find out what the sys package can do for us. Put the
following line of code in your file.
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.
Add the following three lines to your lab3.py file.
print sys.argv print sys.argv * 3 print int( sys.argv ) * 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?
Remove the all of the print statements from the prior steps and type
(or copy and paste) the following code for making a star into your
lab3.py file right after the import statements.
def star(x, y, 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 ) 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.
One thing you discover when you write software is that users do not
always do what they should. They don't always give you enough
information or the correct information. If you want to have a robust
program, you have to check the information coming in to see if it is
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 )
The idea of having default behavior that the user can override is common in computer programs.
Now we're going to explore the range function that is important for
common loops in Python. Open up the Python interpreter in a
terminal. Then call the built-in function range with a single
integer as its argument. 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.
Try getting help on the range function by typing
help( range )
How does it match with what you discovered?
Go back to your lab3.py file. You now know what the range function
does. Create a function called star2( rays, size ) that takes
two arguments. Inside the star function, write a for loop using the
range function with rays as the number of times to loop.
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 ): # 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.
In the star2 function, inside the for loop, set its heading to i * 360 /
rays and then have the turtle go forward by size and backward by size.
def star2( rays, 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?
Modify your main code so that it checks for a second argument from the
command line and assigns an int version of it to the variable rays.
Set the default value for the number of rays to 10. Use the variable as
the first argument to star2.
When you have it working correctly, you should be able to control the length of each ray and the number of rays in each star from the command line.
The final lesson on code organization today is enclosing all of your
top-level code in a main function and the making the execution of that
function dependent upon whether the file was imported into another
file or run from 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.
When you are done with the lab exercises, you may begin the project.