Python Project For Beginners Part 1

Python Project: Beginners Level

GDSC CU
5 min readOct 16, 2021

1. Mad Libs Generator Game

Mad libs generator is a fun game for kids.

In this python game, the user has to enter substitutes for blanks in the story without knowing the story. This game helps students to advance their vocab by the game.

Python Mad Libs Generator

The goal of this task is to develop a Mad Libs Generator python project. In this game, the user has to select a story taking reference from the titles. Then the person has to go into precise phrases like a noun, adverb, verb, food, adjective, etc., in keeping with the requirements. And then the story could be generated.

For this project, we will use the basics of Python and Tkinter.

Prerequisites for the project:

In order to complete the project, you will need the Tkinter library. Write the following command in the command line if you want to get Tkinter. Then wait a few seconds if you want to get to the library.

pip install tkinter

Download Python Mad Libs Generator Code

To download the source code of the project, click on the link given: Python Mad Libs Generator

Project File Structure

Using the source code as a starting point, let’s discuss the project’s structure. The following steps will guide you in building our project:

  • Import modules
  • Create a display window
  • Define functions
  • Create buttons

1. Import Modules

Now that we have downloaded the Tkinter library, it is time to import the packages.

from tkinter import *

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() used for initializing Tkinter
  • geometry() it is used to set the height and width of the window
  • title() use to create the title of the window
  • Label() displays text that users can’t able to modify
  • root it is the name given to our window
  • text It takes the text 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() It is used to take input from the user
  • print() it prints the text.

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() It displays buttons on the window.

root.mainloop() is used to run the program.

Mad Libs Generator Game Output:

Summary of the project:

The project is successfully completed. We used the Tkinter library for this project. You have created your first game. Surely comment down about the project.

This is just part 1. Stay tuned for upcoming project ideas and show some love to this series of blogs :)

--

--