4.4. Making a MadLib story
You might have done MadLib stories when you were a kid. You provide some pieces of information, like a name of a friend, a verb, and a favorite game (for example), and those pieces of information get plugged into a story. Since you don’t know the story beforehand, you’re surprised at what happens to your friend in the story.
Now you want to add more to the story. You want it to say: “Pat called the police who took the witch away.” Adding which of these lines to the end of the program will make that happen? (Hint: It is okay to try each one!)
- realEnding = firstName + " called the police who took the witch away."
- This would only work if you also put
print(realEnding) after this line.
- print(firstName + " called the police who took the witch away.")
- This is a good way to do this since the line that is printed will have the correct first name. You could also make a string named
realEnding first, and then print it.
- print("Pat called the police who took the witch away.")
- This would work. But if you changed the
firstName variable, this line would not change. A different answer is better.
You should really do this: Run this program to see what gets generated, then change some of the variables to make different stories. Try different names, different verbs.
Would the following code print the same story as shown above?
firstName = "Sofia"
lastName = "Diaz"
gender = "girl"
address = "1600 Pennsylvania Avenue"
verb = "burp at"
start = "Once there was a " + gender + " named " + firstName + "."
next1 = "A good " + gender + " living at " + address + "."
next2 = "One day, a wicked witch came to the " + lastName + " house."
next3 = "The wicked witch was planning to " + verb + " " + firstName + "!"
ending = "But " + firstName + " was smart and avoided the wicked witch."
- The only thing that is different is when the lines are printed, but the lines are the same.
- Did you try it? Copy the code into the program area above and run it.
When you print a string (a sequence of characters in a pair of single, double, or triple quotes) in Python it will print the exact characters in the string. When you print a variable it will print the value of that variable.
Put the blocks below into the correct order to print a twist on a famous poem.
print("Roses are red.")
print("Violets are blue.)
print("Sugar is sweet.")
print("And so is Sue.")
Put the blocks below into the correct order to declare the variables and then print the following story. One day Jay went shopping. He wanted to buy shoes. But, he didn’t like any. So, Jay went home.
name = "Jay"
item = "shoes"
print("One day " + name + " went shopping.")
print("He wanted to buy " + item + ".")
print("But, he didn't like any.")
print("So, " + name + " went home.")
Write the code below to calculate and print how many blocks you can travel in an hour if you walk .3 blocks every minute. It should print: “I will travel 18 blocks in an hour if I walk .3 blocks every minute.”
Name each of the values. Calculate the
totalBlocks it will take and print the information.
Discuss topics in this section with classmates.