# loops.py # We know what import does, right? # import math # Let's see what 'while' does in this program. 'while' is used to create # a 'while' loop. That is, 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 (the loop control variable) must change inside the loop body (those two # lines indented inside the 'while' loop) in such a way that the boolean # expression is moving toward to 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! # Here again, we define the function first ... # 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 the human eyes. # print print # Let's write a function that prints when called a log table # 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()" # Actually generate the table by calling the function. # print_table() # Let's 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. # def print_table2(fr, to): x = fr while x < to: print x, '\t', math.log(x) x = x + 1.0 print print 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) # 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 squre 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 6.17 of [DEM] if you want to see more exercise problems.