def inputWords(posTypes,form): words = [] while form: posType = startsWithPosType(posTypes,form) if posType: words.append(input(posType + ' ')) form = form.lstrip(posType) else: form = form[1:] return words def det(noun): if noun[0] in 'aeiou': return 'an' else: return 'a' def startsWithPosType(posTypes,s): ''' Return the longest posType with which s starts. Return the empty string if s does not start with any posType''' # This way, we don't have to bother the user to worry about ordering # the strings in posTypes match = '' for p in posTypes: if s.startswith(p): if len(p) > len(match): match = p return match def generateMadLib(posTypes,form,words): s = '' i = 0 while form: if form.startswith('NEWLINE'): s += '\n' form = form.lstrip('NEWLINE') elif form.startswith('DET'): s += det(words[i]) form = form.lstrip('DET') elif startsWithPosType(posTypes,form): posType = startsWithPosType(posTypes,form) # We have come to a posType: add the next word, and # remove the filler from the form s += words[i] i += 1 form = form.lstrip(posType) else: # No special cases; just add the next character # to the output string, and remove it from form s += form[0] form = form[1:] print(s) def main(): words = [] #Here are all the types of fillers you can use to create your madlib posTypes = ['NOUN','NOUN (PLURAL)','PLACE','ADJECTIVE'] posTypes += ['VERB (SINGULAR SUBJECT)','VERB (PLURAL SUBJECT)','NUMBER','BODY PART'] # Create your form. # Include all spaces in the strings which you want on the output # 'DET' means that a or an should be printed before the next word. # NewLine is just a human-friendly signal that you want a '\n' # form = 'Be kind to your NOUN-footed NOUN (PLURAL). NEWLINE' form += "For a duck may be somebody's NOUN. NEWLINE" form += 'Be kind to your NOUN in PLACE, NEWLINE' form += 'For they may be DET NOUN' words = inputWords(posTypes,form) generateMadLib(posTypes,form,words) main()