Today we will try to clarify for loops. We start by reviewing the while loops. We then say for loops have the following basic structure: for in : We explained that this works like a machine gun. Here's a picture of Upham from Savin Private Ryan with sequences around his neck: http://i29.photobucket.com/albums/c258/elysian2004/Saving%20Private%20Ryan/SAVING_PRIVATE_RYAN-130.jpg Here's such a sequence (of bullets) loaded in a for loop: http://www.thehistorybunker.co.uk/acatalog/M60Long.jpg These are all just metaphors, hopefully they can help you understand for loops. A different, more organized approach would be to simply express a for loop in terms of a while loop. In general: for var in sequence: The equivalent of that is: i = 0 while i < len(sequence): var = sequence[i] i = i + 1 Notice that a for loop simplifies this by eliminating the need for an index (i, in our example). A barber is a for loop whose chair is the loop variable: http://img.alibaba.com/photo/10973212/Luxury_Barber_Chairs.jpg The barber operates as follows: for chair in customers: service person in chair You see that customers, a sequence of actual customers, come into the chair one by one. A dentist is a for loop whose chair is the loop variables. For each patient coming into the chair the dentist handles the specifics of the patient in the chair. Of course, once in a while things can get out of hand: http://www.youtube.com/watch?v=gG8Fwkav18k&feature=related Some more Python examples: for c in "once upon a time": print c This can be implemented with a while as follows: i = 0 while i < len("once upon a time"): c = "once upon a time"[i] print c Or, if you want to be even more aware of its specific parts: index = 0 sequence = "once upon a time" while index < len(sequence): c = sequence[index] print c Let's try solving problem 3 in this list: http://www.cs.indiana.edu/classes/a201-dger/sum2009/weekThree.txt We have seen problems 1, 2, 4 before. Let's worry about them tomorrow. Can we solve problem 6 with what we know so far? Can we solve problem 5 with what is given and what is known so far? We'll work on these three problems in lecture and lab today. Tomorrow we will wrap up with 1, 2, 4. We start here: +---------------------------------------------------------- | import random | | words = ("python", "wednesday", "exercise") | | index = random.randrange(len(words)) | | print words[index] +---------------------------------------------------------- This program randomly selects one of a sequence of strings. The next one does the same but hides the letters, and indicates only the length. +---------------------------------------------------------- | import random | | words = ("python", "wednesday", "exercise") | | index = random.randrange(len(words)) | | secret = words[index] | | dashes = "-" * len(secret) | | print dashes +---------------------------------------------------------- Finally, this program actually plays a crude Hangman game: +---------------------------------------------------------- | import random | | words = ("python", "wednesday", "exercise") | | index = random.randrange(len(words)) | | secret = words[index] | | dashes = "-" * len(secret) | | print dashes | | while dashes != secret: | input = raw_input("Please enter your guess: ") | temp = "" | for i in range(len(secret)): | if secret[i] == input: | temp = temp + secret[i] | else: | temp = temp + dashes[i] | dashes = temp | print dashes | | print "Thanks for playing Hangman." +---------------------------------------------------------- We could easily improve on this program, but we will leave it at that for now. The key to understanding the code above is the second solution to the eggy-peggy problem of yesterday: +---------------------------------------------------------- | line = raw_input("Please type something: ") | | conversion = "" | | for c in line: | if c in "aeiou": | conversion = conversion + "egg" + c | else: | conversion = conversion + c | | print conversion +---------------------------------------------------------- Here's how this program works: +---------------------------------------------------------- | >>> ================================ RESTART ================================ | >>> | Please type something: whatever | wheggateggevegger | >>> line | 'whatever' | >>> conversion | 'wheggateggevegger' | >>> +---------------------------------------------------------- We can understand this program if we start from this: +---------------------------------------------------------- | sum = 0 | | for i in range(10): | sum = sum + i | | print sum +---------------------------------------------------------- Then we make the following changes and think what they mean: +---------------------------------------------------------- | sum = "" | | for i in "nectarine": | sum = sum + i | | print sum +---------------------------------------------------------- Once you figure this one out try this: +---------------------------------------------------------- | sum = "" | | for i in "nectarine": | sum = i + sum | | print sum +---------------------------------------------------------- Does something change? Why or why not? --