MAD LIBS
By Praghya Ramesh
XII B
INDEX
Table of Contents
INDEX ............................................................................................................................................................... I
BONAFIDE........................................................................................................................................................ II
ACKNOWLEDGMENTS ..................................................................................................................................... III
PROJECT SYNOPSIS ........................................................................................................................................... 1
PROJECT ANALYSIS ........................................................................................................................................... 2
1. USING TKINTER – A STANDARD GUI TOOLKIT OF PYTHON ............................................................................................ 2
2. USING THE PYTHON PROGRAMMING LANGUAGE ........................................................................................................ 3
PROJECT LISTING .............................................................................................................................................. 4
MAD_LIBS_STORIES.PY .................................................................................................................................................... 4
MAD LIBS GAME.PY ........................................................................................................................................................ 5
MODULES USED:............................................................................................................................................................ 5
FUNCTIONS USED: .......................................................................................................................................................... 6
BUTTONS USED:............................................................................................................................................................. 9
MAD LIBS GAME.PY ...................................................................................................................................................... 11
MAD_LIBS_STORIES.PY .................................................................................................................................................. 14
SAMPLE OUTPUT ........................................................................................................................................... 16
BIBLIOGRAPHY ............................................................................................................................................... 18
i
BONAFIDE
ii
ACKNOWLEDGMENTS
Our heartfelt gratitude to our Computer Science teacher, Mrs. Nirmala Devi, whose
unwavering patience, kindness, valuable support, and expert guidance played an
indispensable role at every stage of our project. Her insights, constructive feedback,
suggestions, and staunch belief in us were instrumental, and without her dedicated
involvement, this project would not have been possible.
We would also like to use this opportunity to thank the entire Computer Science
faculty for providing us with support, confidence and guidance.
We also express our sincere appreciation to our family members for their enduring
support, constant encouragement, and understanding throughout the entire course
of the project. Their encouragement was a source of strength, and their
understanding made the journey smoother.
Our sincere thanks to Mrs. Shoba Raman, Principal, Vidya Mandir Senior Secondary
School, Mylapore, Chennai, for giving us the opportunity to work on our project.
Thank you all for your invaluable support and contributions to our project.
iii
PROJECT SYNOPSIS
Mad Libs is an interactive word game application that offers users an amusing and
creative experience. The project is based on the party and favorite pastime game,
Mad Libs, a phrasal template word game, where players can fill in blanks with
different types of words such as nouns, verbs, adjectives, etc., to create unique and
often humorous stories.
This project combines the elements of interactivity, storytelling, and humor, making
it an entertaining and engaging experience for players of all ages. Mad Libs is not
only a game but also a creative platform that encourages users to explore their
imagination and sense of humor.
Mad Libs has the potential to leave a positive impact by providing users with a
lighthearted and entertaining experience. Its simplicity and creativity make it a
project that can be enjoyed by a wide audience.
1
PROJECT ANALYSIS
The main objective of Mad Libs is to provide users with an entertaining and
creative word game experience. Users can actively participate in story creation by
inputting words, resulting in dynamically generated and often humorous
narratives.
The application is made entirely with Python, using Python’s standard GUI toolkit,
Tkinter. A separate module is created for storing the story templates. It is
imported as a module and used in the main program.
Python: Core language used for program implementation
Tkinter: Used for the enabling the user-friendly Graphical User Interface (GUI)
Using Tkinter – A Standard GUI Toolkit of Python
With the help of Tkinter, a user-friendly GUI has been crafted. It provides a
seamless and intuitive experience and guides the users through the process of
selecting a story, inputting words and viewing the final story.
• Selecting a Story:
On running the program, buttons with each story title are displayed, along with
an intro to the game. Users can choose from the variety of pre-defined stories,
each with its own set of blanks to be filled.
• Entering Answers- User Input:
After selecting a story, the application prompts users to input words based on
specified categories, such as adverb, plural noun, food, etc. in a different child
window.
• Displaying the Story:
Once the user fills in all their answers, on pressing the button “Create Story!”,
the program displays the final story with the filled blanks in a separate pop-up
window, allowing the user to read and enjoy their customized creation.
• Buttons to Play Again and Close:
Users have the option to play another round by choosing a different story or
close the program, by clicking the respective buttons.
2
Using the Python Programming Language
Python was used for the core programming of the game. It was used to store the
story templates as well as to link to the GUI.
• Story Templates:
The project includes a variety of pre-defined stories, each with designated
blanks for word input. A separate Python module consists of all the pre-set
story templates as a list of dictionaries.
• Story Generation:
Through Python, the words inputted by the user are filled in the story
template, by means of formatting. The entered words are dynamically
integrated into the selected story template, generating a personalized and
often hilarious narrative.
The entire project is based on the concept of formatting.
3
PROJECT LISTING
The application is split into 2 parts-
(a) mad_libs_stories.py
(b) Mad Libs Game.py
mad_libs_stories.py
mad_libs_stories.py is a Python module dedicated to storing a collection of stories
for the Mad Libs Game. Each story in this module is defined as a dictionary,
containing the story template and a list of blanks to be filled by user inputs.
Structure:
The module consists of a list, called stories, of dictionaries. Each dictionary is a
unique story template.
Each story dictionary consists of the following components-
Title: A name or t identifier for the story.
Story: The narrative template with blank spaces marked for user inputs.
Blanks: A list of word types (nouns, verbs, adjectives, etc.) representing the blanks
in the story.
Execution:
In the main project script (`main.py`), this module is imported to access the
collection of stories. The chosen story's title, template, and blanks are retrieved
for story generation based on user inputs.
Example Story Structure:
{
'title': 'Morning Mayhem',
'story': ‘’’It was a {adjective} morning when {noun} decided to {verb} to the
{place}.’’’,
'blanks': ['adjective', 'noun', 'verb', 'place']
}
4
Mad Libs Game.py
The Mad Libs Game.py file serves as the main script for the Mad Libs project. It
orchestrates the overall flow of the application, including user interaction, story
selection, word input, story generation and display.
Dependencies:
Tkinter Library: Used for creating the graphical user interface.
mad_libs_stories.py Module: Contains a collection of story templates and
associated blanks.
Execution:
Users can run the script to initiate the Mad Libs Adventure Game.
Modules Used:
1. Tkinter Library
2. Mad_libs_stories.py
3. sys
5
Functions Used:
1. select_story(x):
This function selects a story from the collection of stories by passing the index
of the story the user selected as a parameter. It extracts a dictionary containing
the title, story template and associated blanks from the list stories, present in
the module mad_libs_stories.py
The variables selected_story, story and blanks are already declared as empty
dictionary, string and list at the beginning and hence, are global variables.
Calling this function, assigns selected_story with the dictionary of the selected
story containing the title, story template and blanks.
This variable is used to assign the story template to the variable story and the
list of blanks to be filled to variable blanks.
It also calls the get_answers() function in the end, so that the user will be
prompted with the blanks to fill.
def select_story(x):
global selected_story, story, blanks
selected_story=stories[x]
story=selected_story['story']
blanks=selected_story['blanks']
get_answers()
2. get_answers():
This function creates a new Tkinter Toplevel window, called ans_window for
the user to input their answers. The wm_transient() method ensures that the
ans_window() pops up on the screen as a child window of the parent window,
main_window.
The global variable, blanks, is used to access the category of words that need
to be filled in the blanks.
It is iterated through and used to prompt the user to answer appropriately.
Their answer is obtained using the Entry method, a member of the Tkinter
6
module.
All the users answers are stored in a dictionary called answers with the blank
description as key, and user answer as value.
Finally, a button called, “Create Story!” will call the function
final_story(answers) as well as destroy the pop-up window, ans_window.
def get_answers():
global blanks
answers={}
ans_window=tkinter.Toplevel(main_window)
ans_window.title('Enter your answers')
ans_window.wm_transient(main_window)
for blank in blanks:
comm=tkinter.Label(ans_window, text='Enter a/an
{}'.format(blank)).pack()
ans=tkinter.Entry(ans_window)
ans.pack()
answers[blank]= ans
create_button=tkinter.Button(ans_window, text='Create Story!',
command=lambda: [final_story(answers), ans_window.destroy()])
create_button.pack()
3. final_story(answers):
This function retrieves the user answers and fills them in the story template in
their respective places.
The answers dictionary is passed as a parameter and contains the entry
widgets.
Hence, a new dictionary d is declared to obtain the entry instances, or the
values held by the entry widgets. To do so, the Tkinter method, get() is used to
get the entry’s current text as a string.
The variable, fin_Story holds the final story, with the user input integrated.
7
A new Tkinter Toplevel window, story_window is created as a pop-up child
window to display the story.
It also has 2 buttons-
Play again, which destroys the story_window and allows the user to play the
game again, and,
Close, which destroys the story_window and calls the game_quit() function so
that the program is quit.
def final_story(answers):
d = {}
for blank, ans in answers.items():
d[blank]= ans.get()
fin_story= story.format(**d)
story_window=tkinter.Toplevel(main_window)
story_window.title('Your Story!')
story_window.geometry('400x400')
story_window.wm_transient(main_window)
disp= tkinter.Label(story_window, text=fin_story).pack()
play_button= tkinter.Button(story_window, text='Play Again', command=
story_window.destroy).pack()
close_button= tkinter.Button(story_window, text='Close', command=lambda:
[story_window.destroy(), game_quit()]).pack()
4. game_quit():
This quits the program.
5. main_window.mainloop():
6. lambda functions in buttons:
8
Buttons Used:
1. Morning_button:
- Story: 'Morning Mayhem'
- Purpose: Initiates the Mad Libs story titled 'Morning Mayhem' when clicked.
2. Camping_button:
- Story: 'Going Camping'
- Purpose: Initiates the Mad Libs story titled 'Going Camping' when clicked.
3. Restaurant_button:
- Story: 'Restaurant'
- Purpose: Initiates the Mad Libs story titled 'Restaurant' when clicked.
4. Piranha_button:
- Story: 'Crossing a Piranha Infested River'
- Purpose: Initiates the Mad Libs story titled 'Crossing a Piranha Infested River'
when clicked.
5. Fairy_button:
- Story: 'A Fairy Tale'
- Purpose: Initiates the Mad Libs story titled 'A Fairy Tale' when clicked.
6. Apples_button:
- Story: 'Apples'
- Purpose: Initiates the Mad Libs story titled 'Apples' when clicked.
7. Summer_button:
- Story: 'Summer Trip'
- Purpose: Initiates the Mad Libs story titled 'Summer Trip' when clicked.
8. Create Story_button:
- Used in: get_answers()
- Purpose: Appears in the answer input window. Triggers the generation of the
Mad Libs story with user-inputted words when clicked.
9. Play Again_button:
9
- Used in: final_story()
- Purpose: Appears in the final story window. Allows the player to initiate the
Mad Libs game again with a different story.
10. Close_button:
- Used in: final_story()
- Purpose: Appears in the final story window. Closes both the final story window
and the main window, ending the game.
10
Mad Libs Game.py
from mad_libs_stories import stories
import tkinter, sys
intro='''Mad Libs is a phrasal template word game. Select a story,
after which you will be prompted to enter nouns, verbs, etc.
These will be used to complete the story. However, you may only
read the story after you have filled the blanks.
The more crazier your word choice, the more funnier the story gets!
Have fun!
'''
main_window=tkinter.Tk()
main_window.title('Mad Libs Game')
main_window.geometry('450x450')
heading=tkinter.Label(main_window, text='Mad Libs', font=('Comic Sans MS', 70))
heading.pack(anchor='center')
desc=tkinter.Label(main_window, text=intro).pack(anchor='center')
selected_story= {}
story= ''
blanks=[]
def select_story(x):
global selected_story, story, blanks
selected_story=stories[x]
story=selected_story['story']
blanks=selected_story['blanks']
get_answers()
def get_answers():
global blanks
answers={}
11
ans_window=tkinter.Toplevel(main_window)
ans_window.title('Enter your answers')
ans_window.wm_transient(main_window)
for blank in blanks:
comm=tkinter.Label(ans_window, text='Enter a/an {}'.format(blank)).pack()
ans=tkinter.Entry(ans_window)
ans.pack()
answers[blank]= ans
create_button=tkinter.Button(ans_window, text='Create Story!',
command=lambda: [final_story(answers), ans_window.destroy()])
create_button.pack()
def final_story(answers):
d={}
for blank, ans in answers.items():
d[blank]= ans.get()
fin_story= story.format(**d)
story_window=tkinter.Toplevel(main_window)
story_window.title('Your Story!')
story_window.geometry('400x400')
story_window.wm_transient(main_window)
disp= tkinter.Label(story_window, text=fin_story).pack()
play_button= tkinter.Button(story_window, text='Play Again', command=
story_window.destroy).pack()
close_button= tkinter.Button(story_window, text='Close', command=lambda:
[story_window.destroy(), game_quit()]).pack()
def game_quit():
main_window.quit()
main_window.destory()
12
sys.exit()
#BUTTONS
Morning_button= tkinter.Button(main_window, text='Morning Mayhem',
command=lambda: select_story(0)).pack()
Camping_button= tkinter.Button(main_window, text='Going Camping',
command=lambda: select_story(1)).pack()
Restaurant_button= tkinter.Button(main_window, text='Restaurant',
command=lambda: select_story(2)).pack()
Piranha_button= tkinter.Button(main_window, text='Crossing a Piranha Infested
River', command=lambda: select_story(3)).pack()
Fairy_button= tkinter.Button(main_window, text='A Fairy Tale', command=lambda:
select_story(4)).pack()
Apples_button= tkinter.Button(main_window, text='Apples', command=lambda:
select_story(5)).pack()
Summer_button= tkinter.Button(main_window, text='Summer Trip',
command=lambda: select_story(6)).pack()
main_window.mainloop()
13
mad_libs_stories.py
#Story templates for Mad Libs
stories= [
{'title': 'Morning',
'story': '''It was a cold {adjective1} day.
I woke up to the {adjective2} smell of a {bird} bathing in
the {room} downstairs to see if i could help with the {word}.''',
'blanks': ['adjective1', 'adjective2', 'bird', 'room', 'word']},
{'title': 'Camping',
'story': '''This weekend, I'm going camping with {name}! I packed my
lantern, bag and {thing}. I am so {emotion} to {verb1} in the tent.
We are going to hike, fish and {verb2}. At night, we will tell stories
about {silly word} and roast {noun}!''',
'blanks': ['name', 'thing', 'emotion', 'verb1', 'verb2', 'silly word', 'noun']},
{'title': 'Restaurant',
'story': '''It was about {number} {measure of time} that I came to
this hotel in a {mode of transport}. There are a lot of {adjective}
{noun1 (plural)} here. I heard that everyone here {verb} {noun2} every day for
breakfast.''',
'blanks': ['number', 'measure of time', 'mode of transport', 'adjective', 'noun1
(plural)', 'verb', 'noun2']},
{'title': 'Crossing a Piranha-infested River',
'story': '''If you're travelling in {place} and find yourself having
to cross a piranha-filled river, here's how to do it {adverb}.
Piranhas are more {adjective} during the day, so cross at night.
Piranhas are attracted to {liquid} and might bite your {part of body}
if you {verb} in the water!''',
14
'blanks': ['place', 'adverb', 'adjective', 'liquid', 'part of body', 'verb']},
{'title': 'Fairy Tale',
'story': '''Once upon a time, there were 3 {adjective} pigs.
Their owner was Snow {colour}. She also had {number} dwarves
who loved her. She was cursed to {verb1} forever. But then
the 3 piggies {verb2 (past tense)} her and her curse broke!
They all went to {place} and {verb3}.''',
'blanks': ['adjective', 'colour', 'number', 'verb1', 'verb2 (past tense)', 'place',
'verb3']},
{'title': 'Apples',
'story': '''Today, we picked apples from {person}'s orchard.
I ate a {colour} coloured apple right from the tree and it tasted
like {food}. Alos, there were {adjective} apples that looked like
{thing (plural)}. We then went on a free {mode of transport} ride
to {place} where we got to {verb} {adverb}. I was so {feeling}!''',
'blanks': ['person', 'colour', 'food', 'adjective', 'thing (plural)', 'mode of transport',
'place', 'verb', 'adverb', 'feeling']},
{'title': 'Summer Trip',
'story': '''Last summer, my dad and my {family member} took me and
{person} on a trip to {place}. The weather is so {adjective1} there!
They are famous for making {thing (plural)} and they do it so {adverb}.
They are also very proud of their big {noun (plural)}. It was a really
{adjective2} trip! In the end, we {verb (past)} back home.''',
'blanks': ['family member', 'person', 'place', 'adjective1', 'thing (plural)', 'adverb',
'noun (plural)', 'adjective2', 'verb (past)']}
15
SAMPLE OUTPUT
16
17
BIBLIOGRAPHY
Geeksforgeeks: https://www.geeksforgeeks.org
Tutorialspoint: https://www.tutorialspoint.com/index.htm
Satck Overflow: https://stackoverflow.com
Realpython: https://realpython.com/python-gui-tkinter/
18