We know this: if _____ : whatever The code above works as follows: a) the condition (_____) is evaluated b) if it evaluates to true we execute whatever (which is a block of code) c) otherwise we move on to whatever follows the if statement Let's look at it again: if _____ : whatever Let's make a change: while _____ : whatever What did we do? We have changed the keyword if with the keyword while. What does that do? The condition will be evaluated and if it's true we do whatever. Then we evaluate the condition again and if it's true we again do whatever. And so on, and so on. As long as the condition turns out to be true we will keep doing whatever. Until when? Do we ever stop? Yes, we stop when the condition (_____) becomes false. At that point we move on to what comes after the while loop in the program. Can we see some examples? i = 0 while i <= 10: print i i = i + 1 What does the code fragment above do? How about this one below: a = raw_input("Type something: ") while a != "bye": print "You typed:", a a = raw_input("Type something: ") When does this second loop end? Let's solve a problem: a) assume you start with a certain amount of money in the bank (a) b) and the bank is giving you yearly interest (b) c) and every month the interest is added and a certain amount of money is withdrawn (c) for living expenses Write a program that asks the user for these three values (a, b and c) and then prints back how long the account will last. Code to be developed in class will be written here: amount = float(raw_input("Initial amount: ")) rate = float(raw_input("Yearly interest rate: ")) / 1200 expenses = float(raw_input("Monthly withdrawal: ")) months = 0 while amount >= 0: months = months + 1 amount = amount + rate * amount amount = amount - expenses print "After", months, "months acount is at", amount print "It will last", months / 12, "year(s) and", months % 12, "months" Note that in some cases the loop never ends. Note also that when it ends the answer is slightly off. It ends a bit too late, that is. We'll polish it later. For now, let's try to get the basic outline constructed. -------------------------------------------------------------------------------------------------------------------------- Python has several kinds of loop constructs. One of them is while. Another one is the for loop. The for loop allows for a certain variable to visit all the elements in a sequence. For example: for a in "what is going on?": print a This allows a to become each one of the characters in the string. We print a to prove that. Strings are sequences. We already knew that (because of the indexing). Lists are also sequences. [2, 3, 1, 0, 6, -1, 4] is a list of integers. We will talk more about sequences next week. range(...) is another useful function. Try range(10). What does it return? What does this code fragment do: for i in range(10): print i What does this code fragment do: for i in range(10): if i % 2 == 0: print i What does this code fragment do: for i in range(10): for j in range(10): print "(", i, ",", j, ")" What's the difference between print "hello", and print "hello"? Once again: what's the difference between print "hello", and print "hello" Note the extra comma in the first example. Now, what does this code do: for i in range(10): for j in range(10): print "* ", print What does this code do: size = int(raw_input("Enter desired size: ") for i in range(size): for j in range(size): print "* ", print In lab we will discuss and develop the scalable pattern problem.