We returned the second homework exams. We also discussed making appointments to retake exams and to ask questions. The script is here: http://silo.cs.indiana.edu:46016/cgi-bin/sum2009a201/schedule We then discussed the following problem: Write a program that reads a line and prints it back with all the vowels preceded by the string "egg". For example: >>> ================================ RESTART ================================ >>> Please type something: hello heggelleggo >>> ================================ RESTART ================================ >>> Please type something: thank you theggank yeggoeggu >>> ================================ RESTART ================================ >>> Please type something: holy guacamole heggoly geggueggaceggameggolegge >>> ================================ RESTART ================================ >>> Please type something: abracadbra eggabreggaceggadbregga >>> ================================ RESTART ================================ >>> Please type something: bear beggeeggar >>> ================================ RESTART ================================ >>> Please type something: egg eggegg >>> ================================ RESTART ================================ >>> Please type something: watermelon weggateggermeggeleggon >>> We emphasized that there are tools listed in chapter 2 that could help us with this problem. The tools in question were: strings and their methods. So we looked over upper(), title(), capitalize(), strip() etc. culminating with replace(..., ... (, ...)). We noticed strings were objects and the methods invoked were acting on specific strings. Examples: a = "bear" b = "Larry Bird" a.upper() --> produces "BEAR", but a is unchanged b.upper() --> produces "LARRY BIRD" but b is unchanged a.replace("e", "") --> produces "bar" and a is still unchanged c = "abracadabra" c.replace("a", "(a)") --> produces "(a)br(a)c(a)d(a)br(a)" c.replace("a", "(a)", 2) --> produces "(a)br(a)cadabra" The first solution for this problem used replace, and a careful sequence of statements: line = raw_input("Please type something: ") prefix = "egg" line = line.replace("e", prefix + "e") line = line.replace("a", prefix + "a") line = line.replace("i", prefix + "i") line = line.replace("o", prefix + "o") line = line.replace("u", prefix + "u") print line We discussed turning this program into a stenographer program, one that removes vowels. Later we said: if the prefix is "aeiou" instead of "egg" this method is ineffective. So we need to use a for loop instead: line = raw_input("Please type something: ") prefix = "egg" for c in line: if c in "aeiou": print prefix, c, else: print c, Here's how this program works: >>> ================================ RESTART ================================ >>> Please type something: whatever w h egg a t egg e v egg e r >>> ================================ RESTART ================================ >>> Please type something: whatever w h egg a t egg e v egg e r >>> ================================ RESTART ================================ >>> Please type something: thank you t h egg a n k y egg o egg u >>> ================================ RESTART ================================ >>> Please type something: hello h egg e l l egg o >>> But we needed to understand for loops first. So we started examining this: line = raw_input("Type something: ") for i in line: print i We said a for loop has the following basic structure: for in : Above is a sequence of one or more statements that may refer to in some way. Note that is indented, like in if and while statements. Here's a picture of a for loop: http://www.dlsports.com/signature_firearms_ar15/pic_2004_12_08_ar15_diagram.gif The is the barrel. The sequence is the magazine. The elements of the sequence are the bullets. A for loop works exactly like a machine gun. We'll start tomorrow expressing for loops using while loops to better understand them.