Activity 2-4

Adapted from Brown University CSCI0931. Used with Permission.

Task 1: Python Functions

Consider the following Python program. The numbers to the left of the green line are just line references, and not part of the program; you wouldn't type them if you were writing this program yourself.


y = 10

x = y + 5



def addOne(t):

  y = t + 1

  return y



z = addOne(x)

z

y

    

Starting at Line 1, go through the program and write down the variable table. What does z output in Line 9? What does y output in Line 10?

Task 2: More Python Functions

Consider the following Python program. The numbers to the left of the green line are just line references, and not part of the program; you wouldn't type them if you were writing this program yourself.


def addOne(x):

  x = x + 1

  return x

x = 2

print("Outside: x =", x)

x = addOne(x)

print("Outside: x =", x)

x = addOne(x)

print("Outside: x =", x)

    

Without actually typing out and running the program, what is the output? Verify it with IDLE.

Task 3: Spotting Buggy Functions

Do this activity with a partner.

  1. Look at the following Python functions. Without actually using the computer, check the following: (1) Does the function actually run? (i.e. will there be an error?) (2) If not, how would you fix it? (3) If yes, does it work properly? If it will run but it won't work properly, why? Can you fix it? The line numbers on the left-hand side are for reference only.
    def addTwo(x,y):
        '''Takes two numbers and returns their sum.'''
        z = x + y
        return x
    
    def subtractTwo(x,y):
        '''Takes two numbers and returns their difference'''
        z = x - y
        return t
    
    def multiplyTwo(x,y):
        '''Takes two numbers and returns their product'''
        z = x*y
        return
    
    def divideTwo(x,y):
        '''Takes two numbers and returns their quotient.'''
        z = x/float(y)
        return z
    
    def addList(myList):
        '''Takes a list of numbers and returns the sum of the elements.'''
        s = 0
        for w in myList():
            s = s + w
        return s
    

Task 4: Build Vocabulary for a text

In this last question, we will finally take a step closer to building a concordance with which we can analyze the LegCo records. We will build the vocabulary for the "Tiger, Tiger" poem.

The outline of the program was given to you in class. You also have one of the helper functions, existsInList, from your last homework assignment.

Do not write your entire program at once! Instead, write each function at a time, and test and make sure that it works properly before you go on! For example, you can test existsInList with the following programming statements:

	 def existsInList(myList, element):
	    # your programming code here
	    
	result = existsInList([1, 3, 4, 5, 6], 2)
	print("Result of existsInList([1, 3, 4, 5, 6], 2) is", result)  # should print False
	
	result = existsInList([1, 3, 4, 5, 6], 5)
	print("Result of existsInList([1, 3, 4, 5, 6], 5) is", result)  # should print True
	
	# Try once with words instead of numbers
	result = existsInList(["cat", "dog", "horse", "kitty"], "dog")
	print('Result of existsInList(["cat", "dog", "horse", "kitty"], "dog") is', result)  # should print True
	
	

Once you are satisfied that your function works, go on to the next one. When you are done writing your functions, then write a programming statements to read in "Tiger Tiger", and print out the size of the vocabulary (e.g. the number of words in the vocabulary). Does your program work properly? What happens if you use this version instead?