CS 0008: Fall 2013 Lab 3 YOUR NAME: YOUR PARTNER'S NAME: To get credit on this lab, attend recitation, work with your partner to answer the questions, and give "reasonable" (if not all correct) answers. Each of you should hand in a hardcopy of your own solution, due at the beginning of next week's recitation. ******************** Part 1: Tracing Code ******************** For these questions, then write down what is printed. Answer the question by tracing through the code on paper, before you run the code. Then, run the code to check your answer. Here is an example of how to trace code: def eg(x,y): x = x + 4 z = 1 z = 5 print("in eg, x,y,z:",x,y,z) return x + y + z def main(): x = 3 z = 4 z = eg(x,z) print("in main, x,z:",x,z) main() Write down a variable when it exists, and update its value as appropriate. Use 'main' and the function name to indicate which variable it is. For example, for the above code: main.x 3 main.z 4 (cross out the 4) 16 eg.x 3 (cross out the 3) 7 eg.y 4 eg.z 1 (cross out the 1) 5 Output: in eg, x,y,z: 7 4 5 in main, x,z: 3 16 Make sure you understand this trace before going on (lab3_1.py.txt)! **Question 1 (lab3_2.py.txt): def main(): x = 1 y = 3 print(x,y) change_up(x, y) print(x,y) def change_up(a, b): a = 0 b = 0 print(a, b) main() **Question 2 (lab3_3.py.txt): def fun1(x,y): x = 30 y = 40 z = fun2(x) w = fun3(fun4(z),y) print("fun1, x,y,z,w:",x,y,z,w) return x + y def fun2(y): print("fun2, y:",y) return y + 1 def fun4(x): print("fun4, x:",x) return x - 1 def fun3(z,y): print("fun3, z,y:",z,y) for i in range(3): print("fun3, i:",i) return z def main(): x = 1 y = 2 z = fun1(x,y) print("main, x,y,z:",x,y,z) main() ***************** Part 2: Functions ***************** ** Question 3 Write these functions. Below are a printTestSumList and main functions which you can use. def sumList(lst): '''lst is a non-empty list of numbers. Return the sum of the numbers in lst.''' # hint: use a for loop; see the examples we did in class on Monday 9/9. ** Question 4 def translate(emoticons): '''lst is a list of emoticons (strings). For each, print it and also an English translation (one line per emoticon). :-) is happy >:( is angry Print huh? for an item on the list that is not one of these. If the list is empty, nothing should be printed.''' # hint: use a for-loop. inside the body of the for-loop, use # an if-elif-else statement. See bio_calc.py.txt on the # schedule for Day 1. The body of the 'else' at the end is # executed if none of the tests evaluates to True. Here is # where you can print huh? # Feel free to test for more emoticons if you want to :-) def testPrintSumList(lst,correctAnswer): myAns = sumList(lst) if myAns == correctAnswer: print("Correct, the answer is",myAns) else: print("My answer is",myAns,"but the correct answer is",correctAnswer) def main(): testPrintSumList([0,1,2,3,4],10) testPrintSumList([1.2,2.3,4.4],7.9) testPrintSumList([0],0) testPrintSumList([-1,4],3) translate([":-)",":-D",">:(",":$"]) translate([":$",":-D"]) translate([">:(",":$"]) translate([]) main()