# Midterm Exam #2 # Review Sheet Hints and Code # 1. There is a quick way to do this one that # doesn't involve a for loop. Think back to # how the word[_:_] command works. # 2. The following lines will come in handy at one point if j not in randnums: count = count + 1 # 3. You can create the sides of the box (tabbed over) via for letter in word: print"\t\t" + letter + " "*len(word) + letter # 4. You'll need to build a new word for this one. Use a for # loop such as for letter in phrase: # and then use an if statement to check whether letter is # not equal to any of a,e,i,o,u. (See the tip at the end # of the review sheet.) If not, then add that letter # to the end of your word, and print it after the for loop. # 5. I'll let you figure most of this out. You'll want all the # action to take place within a while loop, and use the # values.append(number) command. # 6. You will need a for loop such as for j in range(0,len(string)-1): # Now count how many times the characters at positions j and j+1 # are both vowels. (See tip.) Then use an if/else statement to wrap up. # 7. This looks a lot like our database.py program. Check there # for ideas if you need to. # 8. Check back to our homework involving college rankings for ideas. # 9. Try setting up a for loop such as for j in range(0,len(roster)): # and check each entry for a match with the name. # Note that swapping two entries is trickier than # it looks! You need to order your code carefully. # Also, to keep track of whether a match is made # define a variable appear = "no" and then change # appear to "yes" if you encounter a match. Then # set up an if appear = "no": statement at the end. # 10. Here's one way to code this one. def parse(string): wordlist = [] word = "" string = string + " " # we add a final space since we add words to the list # whenever we hit a space in the string for character in string: if character != " ": word = word + character else: wordlist.append(word) word = "" return wordlist # 11. This is relatively straight-forward; I'll let you figure it out. # 12. The following lines of code will come in handy. for letter in string: if letter == " ": space = space + 1 elif letter in vowels: # etc. etc.