# IBM Poem Generator, complete version, nm, 4 April 2008 # This version is a complete model of Emmett William's process in # generating "The IBM Poem," as I understand it, except that, being a # console program, it does not increase the size of the type as words # are re-used. # That could be done, too - for instance, if the output were in HTML # with CSS. vocab = [''] * 26 def expand(word): # A general method to expand a sequence of letters into words expanded = [] for letter in word: expanded.append(vocab[ord(letter) - 97]) return expanded print "Provide a word (a sequence of letters) for each letter from a to z." # Get a vocabulary word (the user types one "at random") for each letter a-z for i in range(26): anyword = '' while anyword == '' or not anyword.isalpha(): anyword = raw_input(chr(97+i) + '=') vocab[i] = anyword.lower() # Ask for "IBM" or another three-letter word or acronym tlw = '' while len(tlw) <> 3 and not tlw.isalpha(): tlw = raw_input('Provide a 3 letter word or phrase: ') tlw = tlw.lower() seed = [tlw] print # Expand three times, printing along the way: # 0. Three-letter word into title # 1. Title into three lines # 2. Each of the three lines into as many lines as there are words newseed = [] for i in range(3): for word in seed: words = expand(word) line = ' '.join(words) print line if i == 0: print newseed += words seed = newseed newseed = []