How to Create Mad Libs game in Python

by | Aug 30, 2021 | Python Projects

In the tutorial, we will build a python project, a simple mad libs game in python that is best for beginners looking to advance their python skills.

Create a mad lib game

Python Mad Libs Game

The main objective of this project is to build a mad lib game by just using core python knowledge. Here in this project, we will allow users to inputs specific words like a noun, adverb, verb, food, animal, adjective, etc., based on the given requirement. And using all the user’s inputs, a story will be generated using python.

You can also try how to develop a GUI Dice Rolling Simulator game using python.

What are mad libs?

In simple words, MadLibs is a game in which you write a story with missing words in it; then, you can ask another player to fill in the blanks. As you are the only one who knows what’s in your story about, the result is often funny and silly.

As per Wikipedia, “Traditional MadLibs are done with paper and ink. But now that we’re in the digital age, we can recreate the game with some simple Python code, and reuse each MadLib over and over.”

Project Prerequisites:

In this python project, you just need to know basic python. That includes.

  • Python print() function
  • Python Input / Output function

What will you learn?

  • End to End development of Python game.
  • Basic Input / Output implementation.

What are the steps to make a mad libs game in python?

Below are the steps to follow to make a mad libs generator python game. Follow every step to get a better understanding of the logic behind the mad libs python project.

GETTING STARTED

To get started with the game, first, we have to write a story that we will convert into a MadLib game python and then implement it in code.

To get you inspired, here’s a quick story:

Most doctors agree that bicycle of your is a/an excellent form of exercise. And a bicycle enables you to develop your leg muscles as well as its increase the rate of a heart beat. More people around the world use bicycles than drive bikes. No matter what kind of person you are, always be sure to wear a/an good helmet. Make sure to have yellow reflectors too!

Which in MadLib will be as follow:

Most doctors agree that bicycle of __verb__  is a/an __adjective__ form of exercise. __verb__  a bicycle enables you to develop your __part_of_body__ muscles as well as __adverb__  increase the rate of a __part_of_body__ beat. More __noun__ around the world __verb__  bicycles than drive __animals__. No matter what kind of __noun__ you __verb__, always be sure to wear a/an __adjective__ helmet. Make sure to have  __color__  reflectors too!

 

Now it is time to give life to our MadLib story by programming.

Step 1: Open a new file in your favourite interpreter or IDE. I go with traditional Python IDLE in the python project.

Step 2: Now, create some prompts using the python input() function to collect inputs from the users in string format.

name = input(“Enter your name:”)

Here string between then quote will be printed only on the display screen whenever we run the program. And to get all the necessary words for our Mad lib game, we will do the same with all the required information we like to collect from the users. 

Step 3: Take input from uses and store them into python variables. Please follow the below code to take all necessary input needed for our game from the user.

verb_1 = input("Enter a verb of choice, and press enter:")
adj_1 = input("Enter a adjective of choice, and press enter:")
verb_2 = input("Enter second verb of choice, and press enter:")
body_part = input("Enter a body part name of choice, and press enter:")
adverb = input("Enter an adverb of choice, and press enter:")
body_part_2 = input("Enter any body name of your choice,and press enter:")
noun = input("Enter a noun of choice, and press enter:")
verb_3 = input("Enter the third verb of choice, and press enter:")
animal = input("Enter name of any animal of choice, and press enter:")
noub_2 = input("Enter an noun of choice , and press enter:")
verb_4 = input("Enter the fourth verb of choice, and press enter:")
adj_2 = input("Enter an adjective of chioce, and press enter:")
color = input("Enter any color name, and press enter:")

So we have collected the necessary inputs from the users; now it is time to implement this input to our story using python variables.

Here, we will implement all the collected words into our story and print them at the runtime. 

Step 4: We will use a string variable to write our story and use the ‘+’ sign before and behind each variable to specify that this is not a string but a variable; it is not that much complicated as it seems; follow the below code.

story = "Most doctors agree that bicycle of" + verb_1 + " is a/an " + adj_1 + " form of exercise." + verb_2 +" a bicycle enables you to develop your " + body_part + " muscles as well as " + adverb + " increase the rate of a " + body_part_2 + " beat. More " + noun + " around the world "+ verb_3 +" bicycles than drive "+ animal +". No matter what kind of "+ noun_2 +"you "+ verb_4 + ", always be sure to wear a/an " +adj_2+ " helmet.Make sure to have  " + color + " reflectors too! "

Step 5: Finally, we will complete our program by printing the story using the print function.

print(story)

Full Code 

Now let add it all together as a final program will look like this.

verb_1 = input("Enter a verb of choice, and press enter:")
adj_1 = input("Enter an adjective of choice, and press enter:")
verb_2 = input("Enter second verb of choice, and press enter:")
body_part = input("Enter a body part name of choice, and press enter:")
adverb = input("Enter an adverb of choice, and press enter:")
body_part_2 = input("Enter any body name of your choice,and press enter:")
noun = input("Enter a noun of choice, and press enter:")
verb_3 = input("Enter the third verb of choice, and press enter:")
animal = input("Enter name of any animal of choice, and press enter:")
noun_2 = input("Enter an noun of choice , and press enter:")
verb_4 = input("Enter the fourth verb of choice, and press enter:")
adj_2 = input("Enter an adjective of chioce, and press enter:")
color = input("Enter any color name, and press enter:")



story = "Most doctors agree that bicycle of" + verb_1 + " is a/an " + adj_1 + " form of exercise." + verb_2 +" a bicycle enables you to develop your " + body_part + " muscles as well as " + adverb + " increase the rate of a " + body_part_2+" beat. More " + noun + " around the world "+ verb_3 +" bicycles than drive "+ animal +". No matter what kind of "+ noun_2 +"you "+ verb_4 +", always be sure to wear a/an "+ adj_2 +" helmet.Make sure to have  " + color + " reflectors too! "



print(story)

 

Time to Play!

So let run our program and test our project by executing the program.

The output of the project

Enter a verb of choice, and press enter: your
Enter an adjective of choice, and press enter: excellent
Enter the second verb of choice, and press enter: and
Enter a body part name of choice, and press enter: leg
Enter an adverb of choice, and press enter: its
Enter any body name of your choice, and press enter: heart
Enter a noun of choice, and press enter: people
Enter the third verb of choice, and press enter: use
Enter name of any animal of choice, and press enter: bikes
Enter a noun of choice, and press enter: person
Enter the fourth verb of choice, and press enter: are
Enter an adjective of choice, and press enter: good
Enter any color name, and press enter: yellow
Most doctors agree that bicycle of your is a/an excellent form of exercise. And a bicycle enables you to develop your leg muscles as well as its increase the rate of a heart beat. More people around the world use bicycles than drive bikes. No matter what kind of person you are, always be sure to wear a/an good helmet. Make sure to have  yellow reflectors to

We hope you guys had fun learning this project, and you can see how we have implemented a simple story into a beautiful game.

Have Fun and Happy pythoning

Download Python Mad Libs Generator Code

Support our work

Buy Me A Coffee