CS 0008: Spring 2015 Lab 2 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 Part 1, you are given some code. You should trace it on paper, and write down the output, before you run the code. When you are done, run the code to check your answers. You can download the files from the schedule. Here is the code in file [lab2_1.py]: 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() WE HAVE TRACED THE CODE FOR YOU, to show you an example: Write down the variables and show the values assigned to them as the code is executed. List the variables under the functions they belong to. Write down the output (in a separate place). For example, for the code above, here is the trace followed by the output. Function eg: x 3 (cross out the 3) 7 y 4 z 1 (cross out the 1) 5 Function main: x 3 z 4 (cross out the 4) 16 Output: in eg, x,y,z: 7 4 5 in main, x,z: 3 16 **Question 1 [lab2_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 [lab2_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: Short Answer ******************** Put your answers here, but be sure to test them out in IDLE to make sure you are correct. ** Question 3 Add instructions after the following to swap the values of x and y. Your code should work for any values of x and y (that is, do not simply assign 10 to x and 5 to y). Hint: you will need three instructions. x = 5 y = 10 ** Question 4 Assign ints to variables x and y (you choose which ones). Write a print statement that prints their values, separated by !!! (that is, there should be three exclamation points between them) ** Question 5 Assign 234556.32345 to x and 432.3333 to y. Print their values on separate lines such that: (1) the comma should appear in the first one, (2) only two decimal places should be printed, and (3) the numbers should lined up. Your code should work for any values of x and y that are less than 1,000,000.00 ****************** Part 3: Function ****************** ** Question 6 Write this function. Below is a main program you can use to test your function. def translate(lst): '''lst is a list of emoticons (strings). For each, print it and also an English translation (one line per emoticon). :-) is happy >:( is angry :$ is embarrassed 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 main(): translate([":-)",":-D",">:(",":$"]) translate([":$",":-D"]) translate([">:(",":$"]) translate([]) main()