Homework 2-2

Due October 8, 2015, 9:00 am


If a problem is marked as “(Independent)”, you may only discuss the problem with course staff. Otherwise, you are free to discuss the concepts that will help you solve the problems with classmates as well as course staff. However, you are never allowed to simply copy answers.

Task 1

Look at the following Python program (the numbers to the left of the line are line references and not part of the program):

numOfPC = 10
numOfMac = 4
inventory = [numOfPC, numOfMac]

def addOne(num):
  result = num + 1
  return result

numOfPC = addOne(numOfPC)
inventory[1] = addOne(inventory[1])
  1. Without actually running the program in Python, write down your prediction of the values of the variables numOfPC, numOfMac, and inventory after the program has executed lines 3, 9, 10, and 11. (Hint: when in doubt, review the way we reasoned about programs in class and follow it religiously).
  2. Now open IDLE. Under the File menu in the top left corner, click on New Window (or simply press Ctrl+N on your keyboard). This should open up a new file as we saw in class. Type in the program.
  3. By inserting some print statements, modify the program so that when you execute it, you can see what are the values of the variables numOfPC, numOfMac, and inventory after lines 3, 9, 10, and 11.
  4. Save your program and name it FirstLast_HW2-2_Task1.py. (You need to hand in this program.) Then press F5 to run the program. Do the outputs match your prediction from the first question? If there is a difference, try to explain it. If not, explain your reasoning. Write this solution, as well as your original guesses in a text file called FirstLast_HW2-2_Answers.txt

Task 2

Copy the stencil code for this assignment to your computer

Write your answers in the text file you used in Task 1. Throughout the stencil code, there are several commented numbers (#1, #2, etc.) scattered around. For each of these break points, we want you to do the following:

~List every user-defined variable and function that currently exists at that line.

~Each variable's type (int, float, string, etc. For lists, specify list type, i.e. list(string), list(int), etc.)

~Each variable's value (for example, 1, 3.14, ["a", "b", "c"], etc.)

If this is still a little confusing, look at the example in the stencil. Once you've finished, double-check your answers by adding print() statements at each break point to see what the variables actually are, like what you did for task 1. Just as in task 1, if the program outputs do not match your prediction, be sure to explain why; you don't have to explain your reasoning if your guesses are correct.


Rename your Python file FirstLast_HW2-2_Task1.py and your text file FirstLast_HW2-2_Answers.txt and share them both with cs0931handinfall2015@gmail.com .

Note: Before you turn in your Python files, make sure they run without any errors (Save your Python file. Then select Run > Run Module or hit F5 on your keyboard)! If nothing appears in the Shell, don't worry as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!