Project: Mad Libs
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Project: Mad Libs
- In this project you build a small story machine.
- It asks for a few words, then puts them into a story.
- You use what you know:
input(), joining strings with+, and string methods.
What you will build
Mia
cat
pizza
The story of Mia
================
Mia has a pet CAT.
It eats pizza every day.
- The first three lines are what the user types. The rest is the story.
Step 1: collect the words
- Each
input()reads one line. Store each word in its own variable. - Join the words with
+, and put the spaces inside the quotes.
Step 2: change a word
.upper()gives a CAPITAL copy:"cat".upper()is"CAT".- Each
printadds one line to the story.
Step 3: a title with a line under it
len(text)counts the characters in a string."=" * 5repeats a string five times:=====.- So
"=" * len(title)draws a line exactly as long as the title.
title = "Hello"
print(title)
print("=" * len(title))
Now build it
- Three steps. Each task starts with your code from the step before.
- Press Run and type the words yourself, then press Check answer.
Step 1: read a name, then an animal, each with input(). Print one sentence. For the inputs Mia and cat, print Mia has a pet cat.
Click Run to see the output here.
Step 2: also read a food. Print two lines: the first sentence with the animal in CAPITALS, then It eats + the food + every day. For Mia, cat and pizza: Mia has a pet CAT. and It eats pizza every day.
Click Run to see the output here.
Step 3: start the story with a title, The story of + the name, and underline it with = signs exactly as long as the title (use len). For Mia the first two lines are The story of Mia and ================.
Click Run to see the output here.