# IBM Poem Generator, nm, 4 April 2008 vocab = ['...'] * 26 # Get a vocabulary word (the user types one "at random") for each letter a-z for i in range(26): anyword = raw_input(chr(97+i) + '=') vocab[i] = anyword.lower() # No error checking yet! Type something with numerals or punctuation and # all bets are off... tlw = '...' # Ask for "IBM" or another three-letter word or acronym while not tlw.isalpha(): tlw = raw_input('Provide a 3 letter word or phrase: ') tlw = tlw.lower() # The while condition and lower() should ensure only a-z occurs # Less than three letters ... should crash the program! # More than three letters can be typed in, and the first three will be used. title = vocab[ord(tlw[0]) - 97] + ' ' + vocab[ord(tlw[1]) - 97] + \ ' ' + vocab[ord(tlw[2]) - 97] print title # The title was obtained by looking up three words corresponding to the three # letters just typed in. firstthree = [''] * 3 for i in range(3): for c in vocab[ord(tlw[i]) - 97]: firstthree[i] += vocab[ord(c) - 97] + ' ' print firstthree[i] # This basically repeats the process, running over the title itself rather # than the three-letter word. # Generation of the remaining lines is left as an exercise. There is one # more step of running over lines 1-3 and generating a line for each word. # In fact, you can generalize the producedure to avoid the code duplication # that I've started, if you like.