Python Mad Libs Generator Game
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
Mad libs generator is a fun game that is usually played by kids.
In this python game user has to enter substitutes for blanks in the story without knowing the story. It will be fun to read aloud the stories after filling the blanks.
Python Mad Libs Generator
The objective of this project is to develop a Mad Libs Generator python project. In this project, the user first has to pick a story by the title of the story. Then the user has to enter specific words like a noun, adverb, verb, food, adjective, etc, according to the requirements. And then the story will be generated.
This is a python project for absolute beginners and is developed using the basic concept of python and tkinter.
Steps to develop Mad Libs Generator Game
Project Prerequisites
Tkinter is a GUI Python library used to build GUI applications in the fastest and easiest way. To install the library, you can use the pip install command in command line:
pip install tkinter
Download Python Mad Libs Generator Code
Please download the source code of mad libs generator project: Python Mad Libs Generator
Project File Structure
These are the required steps to build Mad Libs generator python project:
- Import modules
- Create a display window
- Define functions
- Create buttons
1. Import Modules
from tkinter import *
The first step to build a project is to import the required modules. In this project, we import tkinter module.
2. Create a Display Window
root = Tk() root.geometry('300x300') root.title('DataFlair-Mad Libs Generator') Label(root, text= 'Mad Libs Generator \n Have Fun!' , font = 'arial 20 bold').pack() Label(root, text = 'Click Any One :', font = 'arial 15 bold').place(x=40, y=80)
- Tk() initialized tkinter which means window created
- geometry() used when we want to set the width and height of the window
- title() used to give the title of the window
- Label() widget use to display text that users can’t able to modify
- root is the name which we give to our window
- text takes the text we want to display on the label
- font used to set the size and type of fonts
- pack organized widget in block
- place() used when we want to place widgets in a specific position
3. Define Function
def madlib1(): animals= input('enter a animal name : ') profession = input('enter a profession name: ') cloth = input('enter a piece of cloth name: ') things = input('enter a thing name: ') name= input('enter a name: ') place = input('enter a place name: ') verb = input('enter a verb in ing form: ') food = input('food name: ') print('say ' + food + ', the photographer said as the camera flashed! ' + name + ' and I had gone to ' + place +' to get our photos taken on my birthday. The first photo we really wanted was a picture of us dressed as ' + animals + ' pretending to be a ' + profession + '. when we saw the second photo, it was exactly what I wanted. We both looked like ' + things + ' wearing ' + cloth + ' and ' + verb + ' --exactly what I had in mind') def madlib2(): adjactive = input('enter adjective : ') color = input('enter a color name : ') thing = input('enter a thing name :') place = input('enter a place name : ') person= input('enter a person name : ') adjactive1 = input('enter a adjactive : ') insect= input('enter a insect name : ') food = input('enter a food name : ') verb = input('enter a verb name : ') print('Last night I dreamed I was a ' +adjactive+ ' butterfly with ' + color+ ' splocthes that looked like '+thing+ ' .I flew to ' + place+ ' with my bestfriend and '+person+ ' who was a '+adjactive1+ ' ' +insect +' .We ate some ' +food+ ' when we got there and then decided to '+verb+ ' and the dream ended when I said-- lets ' +verb+ '.') def madlib3(): person = input('enter person name: ') color = input('enter color : ') foods = input('enter food name : ') adjective = input('enter aa adjective name: ') thing = input('enter a thing name : ') place = input('enter place : ') verb = input('enter verb : ') adverb = input('enter adverb : ') food = input('enter food name: ') things = input('enter a thing name : ') print('Today we picked apple from '+person+ "'s Orchard. I had no idea there were so many different varieties of apples. I ate " +color+ ' apples straight off the tree that tested like '+foods+ '. Then there was a '+adjective+ ' apple that looked like a ' + thing + '.When our bag were full, we went on a free hay ride to '+place+ ' and back. It ended at a hay pile where we got to ' +verb+ ' ' +adverb+ '. I can hardly wait to get home and cook with the apples. We are going to make appple '+food+ ' and '+things+' pies!.')
- input() field takes the input from the user
- print() will print the text given in it.
4. Define Buttons
Button(root, text= 'The Photographer', font ='arial 15', command= madlib1, bg = 'ghost white').place(x=60, y=120) Button(root, text= 'apple and apple', font ='arial 15', command = madlib3 , bg = 'ghost white').place(x=70, y=180) Button(root, text= 'The Butterfly', font ='arial 15', command = madlib2, bg = 'ghost white').place(x=80, y=240) root.mainloop()
Button() widget used to display buttons on our tkinter window
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
command is called when the button is click and it is used to call the function
root.mainloop() used when we want to run our program
Mad Libs Generator Game Output
Summary
We have successfully developed the mad libs generator python project. We used tkinter library for rendering graphics on a display window and learn how to create buttons widget and pass the function to the button. In this way, we build this python project.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google
I want the answer of Mad Libs Generator Games code.
Mad libs generator is a fun game that is usually played by kids.
In this python game user has to enter substitutes for blanks in the story without knowing the story. It will be fun to read aloud the stories after filling the blanks.
then post the answer.
thankyouuuu
It shows syntax error in line 3.
Hi shreyash thanks for the comment. Please check and rerun the code.
It showing syntax error in line 4
here is the message :
return self.tk.call(‘wm’, ‘geometry’, self._w, newGeometry)
_tkinter.TclError: bad geometry specifier ” 300×300 “
use config it will not give you any errors
Hello zip thanks for your valuable comment, please check and rerun the code again.
what is a def
def is a python keyword used to initialize a function , it should be followed by the name of the function followed by parenthesis
the code inside the function must be indented
to end setting up a function all you have to do is to de-indent the next line
In python we use def to create or define a function. Def is placed before a function name that is provided by the user to create a user defined function.
This didn’t work at all for me and I put all three in exactly like they say, except changing the inputs and names of things.
This did not work for me. I did exactly as it said, but my editor gave me a syntax error. Please fix.
from tkinter
import
i did this and it gives me an error invalid syntax
VERY HELPFUL AND USEFUL FOR STUDENTS AT THE RIGHT TIME
VERY HELPFUL AND USEFUL FOR STUDENTS AT THE RIGHT TIME
This didn’t work at all for me and I put all three in exactly like they say, except changing the inputs and names of things.
from tkinter
import
i did this and it gives me an error invalid syntax
def is a python keyword used to initialize a function , it should be followed by the name of the function followed by parenthesis
the code inside the function must be indented
to end setting up a function all you have to do is to de-indent the next line
they have tooo many spaces and “”those
from tkinter import *root = Tk()
root.geometry(‘300×300’)
root.title(‘DataFlair-Mad Libs Generator’)
Label(root, text= ‘Mad Libs Generator \n Have Fun!’ , font = ‘arial 20 bold’).pack()
Label(root, text = ‘Click Any One :’, font = ‘arial 15 bold’).place(x=40, y=80)def madlib1():
animals= input(‘enter a animal name : ‘)
profession = input(‘enter a profession name: ‘)
cloth = input(‘enter a piece of cloth name: ‘)
things = input(‘enter a thing name: ‘)
name= input(‘enter a name: ‘)
place = input(‘enter a place name: ‘)
verb = input(‘enter a verb in ing form: ‘)
food = input(‘food name: ‘)
print(‘say ‘ + food + ‘, the photographer said as the camera flashed! ‘ + name + ‘ and I had gone to ‘ + place +’ to get our photos taken on my birthday. The first photo we really wanted was a picture of us dressed as ‘ + animals + ‘ pretending to be a ‘ + profession + ‘. when we saw the second photo, it was exactly what I wanted. We both looked like ‘ + things + ‘ wearing ‘ + cloth + ‘ and ‘ + verb + ‘ –exactly what I had in mind’)
def madlib2():
adjactive = input(‘enter adjective : ‘)
color = input(‘enter a color name : ‘)
thing = input(‘enter a thing name :’)
place = input(‘enter a place name : ‘)
person= input(‘enter a person name : ‘)
adjactive1 = input(‘enter a adjactive : ‘)
insect= input(‘enter a insect name : ‘)
food = input(‘enter a food name : ‘)
verb = input(‘enter a verb name : ‘)
print(‘Last night I dreamed I was a ‘ +adjactive+ ‘ butterfly with ‘ + color+ ‘ splocthes that looked like ‘+thing+ ‘ .I flew to ‘ + place+ ‘ with my bestfriend and ‘+person+ ‘ who was a ‘+adjactive1+ ‘ ‘ +insect +’ .We ate some ‘ +food+ ‘ when we got there and then decided to ‘+verb+ ‘ and the dream ended when I said– lets ‘ +verb+ ‘.’)
def madlib3():
person = input(‘enter person name: ‘)
color = input(‘enter color : ‘)
foods = input(‘enter food name : ‘)
adjective = input(‘enter aa adjective name: ‘)
thing = input(‘enter a thing name : ‘)
place = input(‘enter place : ‘)
verb = input(‘enter verb : ‘)
adverb = input(‘enter adverb : ‘)
food = input(‘enter food name: ‘)
things = input(‘enter a thing name : ‘)
print(‘Today we picked apple from ‘+person+ “‘s Orchard. I had no idea there were so many different varieties of apples. I ate ” +color+ ‘ apples straight off the tree that tested like ‘+foods+ ‘. Then there was a ‘+adjective+ ‘ apple that looked like a ‘ + thing + ‘.When our bag were full, we went on a free hay ride to ‘+place+ ‘ and back. It ended at a hay pile where we got to ‘ +verb+ ‘ ‘ +adverb+ ‘. I can hardly wait to get home and cook with the apples. We are going to make appple ‘+food+ ‘ and ‘+things+’ pies!.’) Button(root, text= ‘The Photographer’, font =’arial 15′, command= madlib1, bg = ‘ghost white’).place(x=60, y=120)
Button(root, text= ‘apple and apple’, font =’arial 15′, command = madlib3 , bg = ‘ghost white’).place(x=70, y=180)
Button(root, text= ‘The Butterfly’, font =’arial 15′, command = madlib2, bg = ‘ghost white’).place(x=80, y=240)
root.mainloop()
Thanks mach for helping the community. Keep growing, keep learning and happy coding.
from tkinter import *root = Tk()
root.geometry(‘300×300’)
root.title(‘DataFlair-Mad Libs Generator’)
Label(root, text= ‘Mad Libs Generator \n Have Fun!’ , font = ‘arial 20 bold’).pack()
Label(root, text = ‘Click Any One :’, font = ‘arial 15 bold’).place(x=40, y=80)def madlib1():
animals= input(‘enter a animal name : ‘)
profession = input(‘enter a profession name: ‘)
cloth = input(‘enter a piece of cloth name: ‘)
things = input(‘enter a thing name: ‘)
name= input(‘enter a name: ‘)
place = input(‘enter a place name: ‘)
verb = input(‘enter a verb in ing form: ‘)
food = input(‘food name: ‘)
print(‘say ‘ + food + ‘, the photographer said as the camera flashed! ‘ + name + ‘ and I had gone to ‘ + place +’ to get our photos taken on my birthday. The first photo we really wanted was a picture of us dressed as ‘ + animals + ‘ pretending to be a ‘ + profession + ‘. when we saw the second photo, it was exactly what I wanted. We both looked like ‘ + things + ‘ wearing ‘ + cloth + ‘ and ‘ + verb + ‘ –exactly what I had in mind’)
def madlib2():
adjactive = input(‘enter adjective : ‘)
color = input(‘enter a color name : ‘)
thing = input(‘enter a thing name :’)
place = input(‘enter a place name : ‘)
person= input(‘enter a person name : ‘)
adjactive1 = input(‘enter a adjactive : ‘)
insect= input(‘enter a insect name : ‘)
food = input(‘enter a food name : ‘)
verb = input(‘enter a verb name : ‘)
print(‘Last night I dreamed I was a ‘ +adjactive+ ‘ butterfly with ‘ + color+ ‘ splocthes that looked like ‘+thing+ ‘ .I flew to ‘ + place+ ‘ with my bestfriend and ‘+person+ ‘ who was a ‘+adjactive1+ ‘ ‘ +insect +’ .We ate some ‘ +food+ ‘ when we got there and then decided to ‘+verb+ ‘ and the dream ended when I said– lets ‘ +verb+ ‘.’)
def madlib3():
person = input(‘enter person name: ‘)
color = input(‘enter color : ‘)
foods = input(‘enter food name : ‘)
adjective = input(‘enter aa adjective name: ‘)
thing = input(‘enter a thing name : ‘)
place = input(‘enter place : ‘)
verb = input(‘enter verb : ‘)
adverb = input(‘enter adverb : ‘)
food = input(‘enter food name: ‘)
things = input(‘enter a thing name : ‘)
print(‘Today we picked apple from ‘+person+ “‘s Orchard. I had no idea there were so many different varieties of apples. I ate ” +color+ ‘ apples straight off the tree that tested like ‘+foods+ ‘. Then there was a ‘+adjective+ ‘ apple that looked like a ‘ + thing + ‘.When our bag were full, we went on a free hay ride to ‘+place+ ‘ and back. It ended at a hay pile where we got to ‘ +verb+ ‘ ‘ +adverb+ ‘. I can hardly wait to get home and cook with the apples. We are going to make appple ‘+food+ ‘ and ‘+things+’ pies!.’) Button(root, text= ‘The Photographer’, font =’arial 15′, command= madlib1, bg = ‘ghost white’).place(x=60, y=120)
Button(root, text= ‘apple and apple’, font =’arial 15′, command = madlib3 , bg = ‘ghost white’).place(x=70, y=180)
Button(root, text= ‘The Butterfly’, font =’arial 15′, command = madlib2, bg = ‘ghost white’).place(x=80, y=240)
root.mainloop()
When I press a button it gives me error
Exception in Tkinter callback
Traceback (most recent call last):
File “/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.11/tkinter/__init__.py”, line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File “/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py”, line 30, in madlib2
adjactive = input(‘enter adjective : ‘)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
EOFError: EOF when reading a line