CS 111
Spring 2018

Old version

This is the CS 111 site as it appeared on May 10, 2018.

Lab 2: Tracing and debugging; practice with lists and functions

FLR 267

If your lab meets in FLR 267, you should begin by following the instructions found here.

Using folders

We strongly encourage you to create a separate folder for each lab and each problem set, so that you can more easily keep track of your work. For example, you could create a folder called lab2 for your work on this lab, and put all of the files for this lab in that folder. If you are working on a lab machine, make sure to create the folder on the Z: drive, so that it won’t be lost when you log out.

Task 0: Tracing Python code – manually and with Python Tutor

Consider the following program:

a = 5
b = 15
s = 'cs111'

s = s[:3]
a = b // a
a = b / a
b = b % 4
s = s[::-1]

print(a, b, a + b, s[0])
  1. To begin, copy the table below onto a piece of paper, and fill in the rest of the table to show how the values of the variables change over time.

    line of code  |  a  |  b  |    s    |
    -------------------------------------
    a = 5         |  5  |     |         |
    b = 15        |  5  |  15 |         |
    s = 'cs111'   |  5  |  15 | 'cs111' |
    s = s[:3]     |     |     |         |
    a = b // a    |     |     |         |
    a = b / a     |     |     |         |
    b = b % 4     |     |     |         |
    s = s[::-1]   |     |     |         |
    

    Using a table in this way is a great way to determine what a program is doing.

    Based on your tracing, what is the output of this program?

  2. Next, let’s see how we can use an online tool called Python Tutor to help us in tracing through a program.

    1. Click on the Python Tutor link in the left-hand navigation bar.
    2. Copy and paste the code above into the empty text box in Python Tutor.
    3. Click the Visualize Execution button below the text box.
    4. Click the Forward button several times, and notice what happens as you do so. In particular, notice the Frames region to the right of the code that shows the current values of the variables.
    5. Experiment with some of the other buttons (Back, Last, etc.) to see what they do.
    6. Once the program has terminated, note the Program output section at the bottom of the page. It shows the values that were printed by the program. Do they agree with your predicted output?

At the end of the lab, you will turn in your paper trace to the TF. When you have finished the trace in Python Tutor, show it to either the teaching fellow (TF) or the course assistant (CA). Feel free to ask them any questions that you have about this problem!

Task 1: Practice with lists

Right-click on the following link, and then choose Download File As or Save Link As in order to download the file: lab2task1.py.

Remember that if you are working on a lab computer, you should save your files on the Z: drive.

Open the file in IDLE, following the procedure outlined in Task 2 of Lab 1. We also encourage you to use the screen layout mentioned in that lab.

Put all of the code for this task in this file.

The problems below ask you to create a few lists using only the following:

Before getting started, you should run lab2task1.py in IDLE using F5. This will make the lists num and info available to you in the Python Shell, so that you can experiment with possible answers in the Shell.

Here are the problems:

  1. Use num and/or info to create the list [8, 7, 3, 9], and assign it to the variable list1. This example has been completed for you and is already in lab2task1.py.
  2. Use num and/or info to create the list [9, 1, 1], and assign it to the variable list2. Follow the same format that we gave you for list1.
  3. Use num and/or info to create the list [4, 1, 1, 6, 5, 0], and assign it to the variable list3.

Feel free to ask the TF or CA for help if you get stuck!

Once you have completed these problems, run your program in IDLE using F5 to ensure that you get the correct output.

Task 2: Debugging and writing functions

  1. In IDLE, use the File -> New Window menu option to open a new editor window for your program, and save it using the the name lab2task2.py. Make sure to specify the .py extension.

  2. Copy the following (buggy) code into that file:

    def mysum(x, y)
    """ takes two numbers and returns their sum """
        total = x + y
        print(total)
    
  3. Save the file using Ctrl-S. Then try to run it using F5. What happens?

  4. This function include two syntax errors – errors that violate the rules of the Python language. See if you can find and fix them, continuing until you are able to run the file without any error messages. Feel free to ask the TF or CA if you need help.

    Note: You should not change the print statement at this point, since it does not have any syntax errors.

  5. Once you have eliminated the syntax errors, test the function from the Shell as follows:

    >>> mysum(44, 67)
    
    >>> print(mysum(10, 7))
    

    What happens? Why?

  6. Fix the function so that both of the above tests work as expected. Hint: The function is supposed to return the sum, not print it!

  7. In lab2task2.py, write a function named sum_double that accepts two integers as parameters. The function should return the sum of the integers, unless the two values are the same, in which case the function should return double their sum. For example:

    >>> sum_double(1, 2)
    3
    >>> sum_double(3, 2)
    5
    >>> sum_double(2, 2)
    8
    

    Be sure to write a docstring for the function.

  8. After writing the function, test it in two ways:

    • Make function calls from the Shell like the ones shown above.

    • Copy the following code into your file:

      def test():
          """ function for testing """
          test1 = sum_double(1, 2)
          print('first test returns', test1)
      
          # Add more tests below
      

      This code provides the beginnings of a test function, with lines that call the function with a set of inputs and print the return value. It’s worth noting that this function does not need a return statement, because its sole purpose is to make test calls and print their results.

      You should:

      • add other test cases to the test function
      • run the Python file
      • call the test function from the Shell (by entering the call test()) and check that you obtain the correct return values for each test case.

Task 3: Tracing a function call

Consider the following program:

def mystery(a, b):
    """ takes two numbers, a and b, and does something with them! """
    print(a, b)
    if a > b:
        c = a + 4
    elif b > a:
        c = b - 2
    else:
        c = -1
    return c

a = 3
b = 4
c = 5
b = mystery(c, a)
print(a, b, c)
  1. Once again, start by tracing the execution of this program on paper. Copy the tables below onto a piece of paper, and fill them in to show how the values of the variables change over time.

    global variables (ones that belong to the global scope)
      a  |  b  |  c
    -----------------
         |     |     
         |     |
    
    local variables (ones that belong to mystery)
      a  |  b  |  c
    -----------------
         |     |     
         |     |
    

    Note that we need two tables: one for the global variables (the ones that belong to the global scope) and one for the local variables (the ones belong to the function mystery. Don’t forget that these are two separate sets of variables, even though they happen to have the same names.

    Based on your tracing, what do you predict will be the output of this program?

  2. Copy and paste the program into Python Tutor, and use it to trace through the code, following the same procedure that you took in Task 0.

    • How many frames do you get in the Frames region of Python Tutor?

    • Do the values in the frames agree with the values in your two tables?

    • Was your predicted output correct? Why or why not?

    • At the end of the program, why is c still 5?

When you have finished both forms of tracing, turn in your paper trace and show your Python Tutor trace to either the teaching fellow (TF) or the course assistant (CA). Feel free to ask them any questions that you have about this problem!

If you have extra time!

Complete any of the problems listed under the Warmup-1 collection of problems on CodingBat. These problems provide good practice with writing functions with lists and strings.

Task 4: Submit your work

You should turn in the paper with your code traces to the teaching fellow.

You should use Apollo to submit the following files:

Make sure to use these exact file names, or Apollo will not accept your files. If Apollo reports that a file does not have the correct name, you should rename the file using the name listed in the assignment or on the Apollo upload page.

Don’t worry if you didn’t finish all of the tasks. You should just submit whatever work you were able to complete during lab.

Here are the steps:

  1. Login to Apollo, using the link in the left-hand navigation bar. You will need to use your Kerberos user name and password.
  2. Check to see that your BU username is at the top of the Apollo page. If it isn’t, click the Log out button and login again.
  3. Find the appropriate lab section on the main page and click Upload files.
  4. For each file that you want to submit, find the matching upload section for the file. Make sure that you use the right section for each file. You may upload any number of files at a time.
  5. Click the Upload button at the bottom of the page.
  6. Review the upload results. If Apollo reports any issues, return to the upload page by clicking the link at the top of the results page, and try the upload again, per Apollo’s advice.
  7. Once all of your files have been successfully uploaded, return to the upload page by clicking the link at the top of the results page. The upload page will show you when you uploaded each file, and it will give you a way to view or download the uploaded file. Click on the link for each file so that you can ensure that you submitted the correct file.