CS 151: Lab #3

Lab Exercise 3: Loops, conditionals and command line parameters

Main course page


  1. On the Desktop, make a folder called lab3. Copy your shapes.py file from project 2 into the folder.
  2. 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 and the date. Save it in the lab3 folder.
  3. After the comment line, write the command to tell python to import the turtle package and the sys package:

    import turtle
    import sys

  4. Fist, let's find out what the sys package can do for us. Put the following line of code in your file.

    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. What do you see? How could you use that capability?

  5. 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 there.

    For example, what if we wanted to use a command-line parameter to control the length of a line? Are there reasonable bounds on the line size? 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.

    distance = 100

    If the user gives you a new value, then use the new value.

    # check for user input
    if len( sys.argv ) > 1:
        distance = int( sys.argv[1] )
    # draw the line and wait
    turtle.forward( distance )

    We'll come back to this capability later in the lab.

  6. 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?

  7. Go back to your lab3.py file. You now know what the range function does. Create a function called star( N, size ) that takes two arguments. Inside the star function, write a for loop using the range function with N 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. The code below is an example.

    def star( N, size ):
        # loop over the list returned by the range function
        for i in range(N):
            print i

    Put a call to the star function with the arguments 10 and distance in your main code section. Run your program and see what prints out.

  8. In the star function, inside the for loop set its heading to i * 360 / N and then have the turtle go forward by size and backward by size.
    def star( N, size ):
        # loop over the list returned by the range function
        for i in range(N):
            turtle.setheading( i * 360 / N )
    	turtle.forward( size )
    	turtle.backward( size )

    What is this going to do? What happens if you give it different command-line arguments?

    How could you use the range function to avoid having to calculate the angle for setheading? What about the following?

    def star( N, size ):
        # loop over the list returned by the range function
        for angle in range(0, 360, 360/N):
            turtle.setheading( angle )
    	turtle.forward( size )
    	turtle.backward( size )

    What will this do?

  9. Modify your main code so that it also checks for a second argument from the command line and assigns an int version of it to the variable N. Set the default value for N to 10.

    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.

  10. 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.

    At the top of your top-level code, put the line def main():, then select all of your top-level code and shift it right. After the main function definition, put the following top-level code.

    if __name__ == "__main__":

    Run your file.

    The above conditional statement will only be true 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.

Once you have finished the lab, go ahead and get started on project 3.