Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 7, 2017 22:25
Show Gist options
  • Save codecademydev/cde8a5f894d2e17577046ebdcdafa0d5 to your computer and use it in GitHub Desktop.
Save codecademydev/cde8a5f894d2e17577046ebdcdafa0d5 to your computer and use it in GitHub Desktop.
Codecademy export
from os import getenv
from textwrap import fill
""" Python MadLibs by Arthur Gordon-Wright
Creates a Mad Libs story based on user input.
User provides words to fill blanks in template story.
Outputs complete story to console."""
#The template for the story
STORY = "This morning I woke up and felt %s because %s was going to finally %s over the big %s %s. On the other side of the %s were many %ss protesting to keep %s in stores. The crowd began to %s to the rythym of the %s, which made all of the %ss very %s. %s tried to %s into the sewers and found %s rats. Needing help, %s quickly called %s. %s appeared and saved %s by flying to %s and dropping %s into a puddle of %s. %s then fell asleep and woke up in the year %s, in a world where %ss ruled the world."
#Global variables and data structures
user = "" #User name
main_char = "" #Main character name
adjectives = []
verbs = []
nouns = []
word_dict = {
"animal" : "animal",
"food" : "food",
"fruit" : "fruit",
"number" : "number",
"superhero" : "superhero",
"country" : "country",
"dessert" : "dessert",
"year" : "year"
}
#Begin: Get user name from shell environment or default to User, welcome message
user = getenv('USER',"User")
print "\nHello %s, welcome to Mad Libs\n" % user
#Select main character name
main_char = raw_input("Enter main character name, or leave blank for your own name: ")
if main_char == "" or main_char == " " or main_char == "\n":
main_char = user
print
#Select adjectives
print "Now input three adjectives."
for i in range(3):
adjectives.append(raw_input("Adjective %d: " % (i+1)))
print
#Select verbs
print "Now input three verbs."
for i in range(3):
verbs.append(raw_input("Verb %d: " % (i+1)))
print
#Select nouns
print "Now input four nouns."
for i in range(4):
nouns.append(raw_input("Noun %d: " % (i+1)))
print
#Select other story words
print "Now enter the key words for your story"
for key in word_dict:
word_dict[key] = raw_input("Enter a %s: " % (key))
print
#Print the final story to console
print "Ok, here goes...\n"
STORY = fill(STORY,50)
print STORY % (adjectives[0], main_char, verbs[0], adjectives[1], nouns[0], nouns[1], word_dict["animal"], word_dict["food"], verbs[1], nouns[2], word_dict["fruit"], adjectives[2], main_char, verbs[2], word_dict["number"], main_char, word_dict["superhero"], word_dict["superhero"], main_char, word_dict["country"], main_char, word_dict["dessert"], main_char, word_dict["year"], nouns[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment