CS 0008: Fall 2013 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 ******** Please type your answers in here. Python has built-in functions abs, round, int, and float. Using these functions, write statements to do the following (test your answers in the python shell) Q1: Convert 34.7 to an integer by truncating Q2: Convert 3.7 to an integer by rounding Q3: Take the absolute value of -86, then convert it to a floating point number Q4: Assign ints to variables x and y (you choose which ones). Write a print statement that prints their values, separated by ! Q5: 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) their values should be lined up. Your statements should work even if we change the values of x and y (to numbers that are not greater than the first one). ********* PART 2 ******** Copy the rest of this file to a file named yournamelab2.py. Write and test the following functions (in that file). Work on one at a time. Demonstrate your code for one of us. def celsius_to_fahrenheit(celsius): '''Return celsius but converted to fahrenheit. Your answer should be rounded to the nearest integer. The formula to use is celsius * 1.8 + 32''' def convert_mileage(miles_per_gallon): '''Return miles_per_gallon, but converted into kilometers per liter. Your answer should be rounded to the nearest integer. 1 mile is 1.60934 kilometers, and 1 gallon is 3.78541 liters. Example: convert_mileage(35) is ''' # For the following two, the operators // and % will be very helpful. # These will take some problem solving. (Note, they will help with # assignment 1) def remove_rightmost_digit(num): '''Return int num, but with the rightmost digit removed. Example: remove_rightmost_digit(4235) returns 423''' def get_rightmost_digit(num): '''num is an int. Return the rightmost digit of num. Example: get_rightmost_digit(4235) returns 5''' def test(function,function_name,argument,correct_answer): '''This is already written for you. Don't worry if you can't understand it.''' str1 = function_name + "(" + str(argument) + ") = " + str(function(argument)) print(format(str1,"<35s"),end=" ") if function(argument) == correct_answer: print("Correct") else: print("The answer should be",correct_answer) def main(): # You should create test cases before you ever start programming # "test" is a function you can use to test the functions here. test(celsius_to_fahrenheit,"celsius_to_fahrenheit",32,90) test(celsius_to_fahrenheit,"celsius_to_fahrenheit",0,32) test(convert_mileage,"convert_mileage",35,15) test(convert_mileage,"convert_mileage",0,0) test(remove_rightmost_digit,"remove_rightmost_digit",4235,423) test(remove_rightmost_digit,"remove_rightmost_digit",5,None) test(get_rightmost_digit,"get_rightmost_digit",4235,5) test(get_rightmost_digit,"get_rightmost_digit",5,5) main()