Skip to content

Instantly share code, notes, and snippets.

@plumps
Created February 22, 2017 22:47
Show Gist options
  • Save plumps/3329a611a0e81a8b440a8dfe3c385af3 to your computer and use it in GitHub Desktop.
Save plumps/3329a611a0e81a8b440a8dfe3c385af3 to your computer and use it in GitHub Desktop.
automatetheboring shit: chapter 8, madlibs
# Create a Mad Libs program that reads in text files and lets the user add
# their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB
# appears in # the text file. For example, a text file may look like this:
# The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
# unaffected by these events.
# The program would find these occurrences and prompt the user to replace them.
# Enter an adjective:
# silly
# Enter a noun:
# chandelier
# Enter a verb:
# screamed
# Enter a noun:
# pickup truck
# The following text file would then be created:
# The silly panda walked to the chandelier and then screamed. A nearby pickup
# truck was unaffected by these events.
import sys
import re
def markerrepl(matchobj):
marker = matchobj.group(0).lower()
return input(f"Enter a(n) {marker}: ")
sentence = open(sys.argv[1]).read()
new_sentence = re.sub("(ADJECTIVE|NOUN|(AD)?VERB)", markerrepl, sentence)
print(new_sentence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment