=== Make sure you bookmark or otherwise make it easy for you to get to the schedule. Check it all the time. Keep up with the lectures and readings. The stuff on the website and the book are your references. Note the reading assignments on the webpage. Go through the code on the website before the next class. Ask questions. Let's look at the end of the lab - the part that says copy the rest of this to a file. These are function headers. They have no bodies yet! Your job is to write the bodies of these functions. The ''' ''' are called "docstrings". They are comments describing what each function does. Let's do the first one - in the editor. 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''' return round(celsius * 1.8 + 32) That is simply the function definition. Let's call it, in the main program. x = celsius_to_fahrenheit(29) print(x) We can check this in google: 29 celsius in fahrenheit. Yup! ** while you finish this lab - it is due on Wed - you can test your functions like this. Just call it. If this isn't clear to anyone, please ask. Now, let's look at the rest of the file. 3 more functions for you to write. The last two are not long. But perhaps tricky. Get into the python shell and play with // and % until you see what to do. That means, try them out with examples which you make up. test - I wrote this for you. You don't need to do anything to it. It is called with a function, the name of the function (so it can be printed), an argument to pass to the function, and the correct answer. It calls your function on that argument, and prints out whether you got the right answer or not. def test(function,function_name,argument,correct_answer): So, look at main: It is a function. We call it on the last line. def main(): # You should create test cases before you ever start programming # "test" is a function you can use to test the functions here. *** So, this passes your function celsius_to_fahrenheit to "test". The argument is 32. The answer should be 90. If your answer is correct, test will print "correct". If not, it will print: The answer should be 90. *** Evaluting whether or not your code is correct is a big part of programming. Before you start programming, come up with test cases. E.g., examples. Then, when you run your code, you will know whether it is correct or not. 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() *** Wed's lab is due at the beginning of the class on Wed. Fri's lab is due at the beginning of the class on Fri. ====Problem Solving mod Use? Suppose we are given seconds, and we need figure out how many hours, minutes, and seconds we have. First step: make sure you understand the problem. Come up with test cases. Let's suppose our test case output is: 2 hours, 2 minutes, and 5 seconds. There are 60 seconds in a minute, and 60 minutes in an hour. In an hour, there are 60 * 60 seconds = 3600 In 2 hours, there are 2 * 3600 seconds = 7200 There are 60 seconds in a minute, thus in 2 minutes, there are 120 seconds. **Test case 1: input: 3600 + 120 + 5 = 7325 output: 2 hours, 2 minutes, and 5 seconds special cases: **Test case 2 exactly one hour input: 3600 output: 1 hours, 0 minutes, and 0 seconds **Test case 3 less than an hour (do output first) input: 35 * 60 + 3 = 2103 output: 0 hours, 35 minutes, 3 seconds We are going to break this down into 4 parts. Once we solve each part, we will forget about it. focus on one part of the problem at a time; Part 1: ask the user for the total number of seconds. Put that value in variable "seconds". E.g., in test case one, seconds = 7325 Part 2: how many complete hours are there? (revisit test cases) secondsInAnHour = 60 * 60 completeHours = seconds // 3600 This will drop all the extra seconds! Part 3: (revisit test cases) calculate leftoverCompleteMinutes This one is a bit tricky! Well, this might be useful: LEAVE UP! (A) totalCompleteMinutes = seconds // 60 This drops the extra seconds. E.g., the 5 in test case one. What we want is the complete minutes that are leftover after we account for the hours. # hours ------- 60|totalCompleteMinutes 60 * #hours ----------- remainder! (B) LEAVE UP! (B) leftoverCompleteMinutes = totalCompleteMinutes % 60 **Test case 1: input: 3600 + 120 + 5 = 7325 output: 2 hours, 2 minutes, and 5 seconds totalCompleteMinutes is 120 (2 hours) + 2 (minutes) A: test 7325 // 60 in the python shell Look at the integer division for this case: 2 ------- 60|122 120 ----------- 2 (B) 122 % 60 (B) = 2! That's right. Part 4: # Now, how about the seconds? We want the seconds left over, # after accounting for the hours and minutes. totalCompleteMinutes --------- 60 | seconds 60 * totalCompleteMinutes ------- leftoverSeconds! remainder (C) (C) leftoverSeconds = seconds % 60 [time_converter.py] ==== future_value.py Let's calculate future value of an investment, given APR (annual percentage rate), current value, and number of years into the future. *** I did this very slowly. Repeated myself alot. Except for the top people, the pace seems needed. There are direct formulas to use. We'll do it with a loop. how do we solve this? value = current_investment Each year - the value increases by apr * last year's value e.g. - apr = .06 value = 1000 after year 1: 1000 + .06 * 1000 = 1060.00 after year 2: 1060 + .06 * 1060 = 1123.60 after year 3: 1123.60 + .06 * 1123.60 = 1191.016 and so on OK? NOTE: the above a good test case, so let's save the values. Test case 1: current_value = 1000, apr=0.6,years=3 result = 1191.016 We can simplify the calculation: 1000 + .06 * 1000 = 1000 * (1 + .06) *** Let's start our program in the editor *** (so that we can put it in a file and save the definitions.) def future_value(current_value,apr,years): new_value = current_value # start things off!! 'Initilization' how many calculations? exactly 'years' We can use what is called a for-loop. for variable in sequence-of-values: body In python, there are different types of sequences of values. here is one: a list Back to the python shell: >>> for i in [1,3,5]: ... print(i) ... 1 3 5 [1,3,5] is a list. Those three items, in that order. executed 3 times - the len of the list. First, i = 3; execute body Next, i = 4; execute body Finally, i = 5; execute body So, we will do a calculation, one for each year. range(0,years) - built in function. This generates a sequence of values, one at a time, from 0 to years-1 (we'll see later in the course why it is years-1 but not years) years = 3 count = 0 for i in range(years): count = count + 1 print(count) The for-loop is executed exactly "years" times. years = 10 # suppose years has this value range(years) for i in range(years): *** 'i' is a variable whose name we made up. *** the for loop will step through the values from 0 to 9 one a time year = 1 year = 2 year = 3 and so on OK, now we have the initilization and the loop set up. How do we write the calculation? everyone write something down in their notes new_value = new_value * (1 + apr) assignment: evaluate the RHS - get's contents of new_value and apr and performs the arithmetic. THEN: places that value back in new_value, clobbering the value it used to have. That's fine, we only need the new value. [leave off the return new_value as a bug] Let's test it. We have a test case above future_value(1000,.06,3) run the program. nothing happens. We called future_value, but didn't save the value, or print it, etc. print ("The future value of a 1000 investment with APR .06 after three years is",future_value(1000,.06,3)) Still doesn't do anything Need the return value! Test it with our test case - it worked! We also should test with special cases. How about 0 years? 1 year? APR of 0? DEBUGGING: you test it on a general/typical case, like the one above, and then test on all the special cases. While you are writing your program you want to design test cases. *If you are going to use the program for something, you want to be sure it is correct! Wouldn't want to make investment decisions on a buggy program! special cases. How about 0 years? 1 year? APR of 0? also, let's make our job easier by writing a print function def print_future_value(current_value,apr,years,future_value): print "The future value of a", current_value, "investment with APR", apr, "after three years is",future_value # should be 1191.016 test1 = future_value(1000,.06,3) print_future_value(1000,.06,3,test1) CLEAN THIS UP; MAKE IT python 3.3; be sure you know what you are typing into the editor. OK, we could call this in a more convenient way print_future_value(1000,.06,3,future_value(1000,.06,3)) How will this execute? python will start to execute print_future_value. Before it can execute, it has to have all of its arguments. It will get to future value, and then go execute that. Yeah, it works! Ok, now let's call it on two different CDs and then total up the two values. Suppose this will be a longer program, and we need to save the total value in variable 'total' Let's try: future_value(1000,.06,3) future_value(1500,.03,3) total = # oops - the above just call the function, and throw the value away #assuming we need to save the cd values for later in the program: cd_1_val = future_value(1000,.06,3) cd_2_val = future_value(1500,.03,3) total = cd_1_val + cd_2_val #Suze Orman example - 45 year old woman with $74K in retirement to # buy a $6400 pool? # Denied! if she invests the money now at 4% and retires at 65, # she will have this much more money print_future_value(6400,.04,20,future_value(1000,0,3)) She would be giving up 8000 (or more, if she gets a higher rate!) This is a very simple program, but still useful! Ok, so that gave us practice with solving a problem, function definitions, function calling, *testing*, and for-loops. ====