Skip to content

Instantly share code, notes, and snippets.

@ViveK-PothinA
Created January 24, 2022 06:36
Show Gist options
  • Save ViveK-PothinA/03bcc6496452c090bd4e0e5f28dfb2a7 to your computer and use it in GitHub Desktop.
Save ViveK-PothinA/03bcc6496452c090bd4e0e5f28dfb2a7 to your computer and use it in GitHub Desktop.
Python script to play Mad Lib
import random
def fetch_mad_libs_list(filename=None):
mad_libs_list = []
if (filename is None):
mad_libs_list = [["Pizza was invented by a {0} {1} chef named {2}. \nTo make a pizza, you need to take a lump of {3}, and make a thin, round {4} {5}. \nThen you cover it with {6} sauce, {7} cheese and fresh chopped {8}. \nNext you have to bake it in a very hot {9}. \nWhen it is done, cut it into {10} {11}. \nSome kids like {12} pizza the best, but my favorite is the {13} pizza. \nIf I could, I would eat pizza {14} times a day." , ["adjective", "nationalitiy", "same nationality first name", "food ingredient", "adjective", "3d shape", "adjective", "adjective", "plural vegetable", "kitchen appliance", "number", "2d shape", "cheese type", "cheese type", "number"]], ["Meet our hero {0}, a super-intelligent {1}. \nA run-in with the {2} military leads him to create his alter-ego {3}, a {4} {5} giant capable of great destruction. He {6} battles the military with his girlfriend {7}. \nEventually it is discovered that our hero's long-time colleague {8}, distinguished by his {9} is trying turn {10} into a weapon, leading to a climatic (if pointless) battle in downtown {11} with an evil version of the same giant alter-ego called {12}. \nEventually the enemy is subdued by {13}ing him with a {14}. \nIn the final reel, {15} appears to propose joining him in a {16}.", ["silly name", "unrealistic profession", "country", "another silly name", "color", "adjective", "adverb", "third silly name", "fourth silly name", "facial feature", "second silly name", "US city", "one more silly name", "verb without 'ing'", "noun", "another silly name", "place"]], ["Press {0}, Press {1}, Press {2} \nHi, I'm {3} about the {4}. \nSure, I'll {5}. \n\n....{6}..... \n\nHello, I'm calling about the {7}. \nYes, I can {8}. \n\n...{9}... \n\nSorry, {10}. \nI didn't realize the music had stopped playing. \nNo, I haven't tried turning the {11} off and turning it back on again. \nI'll give it a try, but-well, that was {12}.", ["single digit number", "single digit number", "single digit number", "verb", "noun", "verb", "lyric to a song(line 1)", "noun", "verb", "lyric to a song(line 1)", "American first name", "noun", "adjective"]]]
else:
with open(filename, 'r') as f:
for row in f:
mad_libs_list.append(row)
return mad_libs_list
def generate_mad_lib(index, mad_libs_list):
selected_mad_lib = mad_libs_list[index][0]
selected_mad_lib_variables = mad_libs_list[index][1]
user_input_list = []
for variable in selected_mad_lib_variables:
user_input_list.append(str(input(f"\nPlease provide {variable}: ")))
print("\n\nHere is your Mad Lib, have fun reading it.\n\n")
print(selected_mad_lib.format(*user_input_list))
if __name__ == "__main__":
mad_libs_list = fetch_mad_libs_list()
print("\n\nHello, let's get started. PLease provide the necessary inputs.")
random_index = random.randint(0, len(mad_libs_list)-1)
generate_mad_lib(random_index, mad_libs_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment