BUILDING A SIMPLE MADLIB WITH PYTHON — #PythonBeginnerSeries
INTRODUCTION:
MAD LIBS is one of the world’s fun word games. Some would even call it one of the ‘GREATEST WORD GAME”. If you enjoy playing around with words, then you probably agree with this phrase.
MadLibs is a phrasal template word game where one player prompts others for a list of words to substitute for blanks in a story/poem/rhyme before reading aloud. The game is frequently played as a party game or as a pastime(Wikipedia).
It is a game where there are neither losers or winners. A game that fills the room with laughter and creates such a relaxing environment, something every one of us needs from time to time for a healthy mental state.
In this article, I am going to share a simple way of creating this game in Python. As a beginner or expert developer, we do need that little fun. Even if you have no coding experience at the moment while reading this article, you will be able to grasp a few concepts and a basic understanding of Python Programming.
HOW IT IS PLAYED
Most often, the game has a simple sequence around it, the aim might be to form a simple poem/rhyme such as the one below, or a short story, just for fun.
“Roses are red, Violets are Blue, Sugar is sweet and So are you”
It could something fun as shown below:
Sounds interesting right!
AN INTRODUCTION TO THE PYTHON PROGRAMMING LANGUAGE
A brief introduction to Python Programming.
- Python is a powerful High-level interpreted “programming Language”.
“Please reference one of the links below to understand the difference between a compiled and an interpreted language”.
- It is a general-purpose language primarily used for DataScience, Machine Learning, Web Development, Game development et cetera.
- Python is an Object-oriented Programing language.
- Created by Guildo Van Rossom and first released in 1991
- Latest Version 3.8.4
Please note that Python 2 and associated versions have been depreciated(no longer supported). Hence take note to only download Version 3.
WHY LEARN PYTHON
- Python is one of the most popular programming languages in the world.
- It is open-source and has great community support.
- It is fun and easy to learn.
- Python is very powerful and easily integrates into most platforms/Frameworks.
- Can be used to automate daily tasks ( Check out the link to “Automate the Boring Stuff with Python” Below)
- It is used by most popular companies such as Google, Youtube, Netflix, Amazon et cetera.
HOW TO INSTALL PYTHON
Python can be installed in several ways. In this article, I will focus on installation from the official website.:
Download and install from the official website
i. Go to the official website “http://www.python.org”
ii. Click on download on the homepage.
iii. Choose your operating system( Windows or Mac os)
iv. Download the latest version(3.8.4)
v. Locate the downloaded file on your browser or download folder on your pc.
vi. Double-click to run and install.
viii. Choose Install Now, also check the ‘Add Python 3.8 to PATH box. Follow the on-screen prompt.
BUILDING A SIMPLE MADLIB PROGRAM WITH PYTHON
Let’s create the Mad Lib program, and then I will explain the concepts.
STEPS:
- Open the Python IDLE(Integrated Development Environment) on your pc, or any other editor of your choice.
- Create a new file and save it as “madlibs.py” (Hint: “.py” is the file extension name for all Python files)
- Type the following line of code:
4. Run the program. You can do this by either clicking on the ‘run’ icon or pressing f5 on the keyboard(Not all systems support the f5 option).
The program is displayed as follows:
Awesome right!
Now let’s explain the concepts used.
- The ‘#’ symbol:
This is used for making comments in Python. As a Programmer, it is good practice to add comments to your codes. Comments are simply a way of telling someone what a particular line of code does. Not only does it help someone else understand your code, but it also helps you keep track and remember why you wrote a particular line of code. Any sentence started with the ‘#’ symbol is automatically recognized as a comment in Python. Hence it does not get executed as a code. It is used for single-line commenting.
2. The input() function:
This is used to accept responses/input from a user in Python. Just as the name implies ‘input’, collecting responses and storing it in a variable ( Oops! I just mentioned another concept).
3. A Variable:
Variables are containers for storing data values/information. The data can be text or numbers. The stored value can then be referenced using the variable’s name. Data to be stored in a variable is assigned with “=” equal to operator, also called the “assignment operator” in Python. Just as we did in the program earlier, we requested for a response using the input function, then stored that response by assigning to a variable called ‘name’. See some other variables below:
a = 5
name = ‘Mardiyyah!’
message = ‘Hello World!’
4. Data Types: Data types are a category of values in Python. It is similar to how we have different types of food and different preparation methods. The data types in Python are:
- Strings: Strings are text like values, enclosed in quotes “”, ‘’
Example: ‘Hello World’, ‘Hi’, ‘My name is’, ‘Welcome’.
Any text-like values in Python are referred to as strings. And they are represented using the double “ ” or single quotes ‘ ’ in Python.
It is a good coding practice to stick to one of the two methods. I.e either use the single or double-quotes.
However, if we have sentences such as, for example ‘it’s mine’, ‘Who’s there?’, ‘what’s your name’?. Then Python gets confused seeing three quotes, so it gives a syntax error. In order to solve this problem, we use double quotes outside the string and a single quote in the word. Take a look at an example below:
So the data type stored in the variable ‘name’ in the Madlib program is a string. Why? because by default, the input function takes in and outputs string data types.
5. The print() function: The print() function is used to display a message in python. It prints/outputs a message or a command inside it.
Just as seen earlier, we used it to display the Program created. The print function has so many attributes and can do a lot more. Make reference to the links below for a broader understanding
Great! Hopefully, you just successfully created your first Python Program. Do read up the resources below for more understanding. I have attached the link to the GitHub repository for the code used below. I hope you will explore further and create something more exciting.
Thank you for reading!
REFERENCES AND RESOURCES
FIND ME ON SOCIAL MEDIA
LinkedIn: https://www.linkedin.com/in/aminah-mardiyyah-rufa-i/
Twitter: @diyyah92