# loops.py # We know what import does, right? # import math # Let's see what 'while' does in the program below. 'while' is used to create # a 'while' loop. That is, the body of the while loop, the two lines indented # inside the 'while' loop, will be executed repeatedly as long as the boolean # expression (n > 0) evaluates to True. # # So, for this style of loop to terminate eventually the value of # n (a loop control variable in this case) must change inside the loop body # in such a way that the boolean expression is moving toward evaluating to # True. # # If you call countdown with a negative number initially, what would happen? # Well, it will get into an infinite loop... sort of... it will run until # the computer gets into a corrupted state or it runs out of memory depending # on how the underlying interpreter behaves. At least you see a possible # problem! # If you do not update the value of the loop control variable inside the # while loop, we will end up with a similar result. # Here again, we define the function first ... # # precondition: n > 0 [the condition that must satisfy at the time the function # is called, i.e., before it is executed.] def countdown(n): while n > 0: print n n = n - 1 print "Blastoff!" # Then, we call (invoke/use/apply/execute/run) the function ... # countdown(5) # To generate a couple of blank lines in the output on the screen to make # the output format a little more pleasing to human eyes. # print print # Let us write a function that prints when called a log table. # The character sequence '\t' generates a tab character. # def print_table(): x = 1.0 while x < 10.0: print x, '\t', math.log(x) x = x + 1.0 # An annotation as part of output (human readability is important!) # print "print_table()" # Finally, generate the table itself by calling the function. # print_table() # Let us improve the earlier definition of print_table by making it more # general. We will parameterize the function by allowing the 'from' value # and the 'to' value to be user definable as the function is called. This # is one of the very important techniques in programming - generalization! # abstraction! That is, we abstract with functions and generalize them # when it makes sense to do so. # def print_table2(fr, to): while fr < to: print fr, '\t', math.log(fr) fr = fr + 1.0 print print # An annotation in the output for readability again # print "print_table2(10.0, 20.0)" # As before generate a table, but using two values as actual arguments for # the two parameters that we introduced in the definition. # print_table2(10.0, 20.0) # What would happen if you called it this way? # print print print "print_table2(30.0, 20.0)" print_table2(30.0, 20.0) # Exercise 1: # # Write a function that prints a table of a number, its square, and its square # root in a range defined by two numbers. For example, numbers between 10 and # 20 and their squares and square roots in a table. Once the function is # defined, call it and be sure that your program generates the table correctly. # Exercise 2: # # Write a function that reads two numbers from keyboard and use those two # numbers as lower and upper limits of the range that you would need for the # function that you wrote in Exercise 1 above. If the input values that your # function reads in are invalid, e.g., first number is larger than the second, # your program must read a pair of numbers repeatedly until you have two valid # numbers. Once you have a good pair of numbers, then you can call the function # defined in Exercise 1 above. To make it a little more interesting, define # a separate function that determines if two given numbers are valid or not, # and use it as part of your reader function. Yes, you could easily do all that # in a single function, but this will give you a chance to practise with # multiple functions that make up a program. In fact, as you will learn later # if you continue to write more complex programs, this is a good way to deal # with complexity in your program design. # Exercise 3: # # See Section 7.9 of [Downey] if you want to see more exercise problems.