3.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.
- 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.
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!)
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.
- Yes
- The only thing that is different is when the lines are printed, but the lines are the same.
- No
- Did you try it? Copy the code into the program area above and run it.
Would the following code print the same story as shown above?
1firstName = "Sofia"
2lastName = "Diaz"
3gender = "girl"
4address = "1600 Pennsylvania Avenue"
5verb = "burp at"
6start = "Once there was a " + gender + " named " + firstName + "."
7print(start)
8next1 = "A good " + gender + " living at " + address + "."
9print(next1)
10next2 = "One day, a wicked witch came to the " + lastName + " house."
11print(next2)
12next3 = "The wicked witch was planning to " + verb + " " + firstName + "!"
13print(next3)
14ending = "But " + firstName + " was smart and avoided the wicked witch."
15print(ending)
- Mali is Mali
- There are no double quotes around the last Mali so it will use the value of the variable Mali.
- Mali is 5
- The first Mali is in double quotes so it will print the string Mali and the second Mali is not in double quotes so it will print the value of the variable Mali.
- 5 is Mali
- The first Mali is in double quotes and the second is not.
- 5 is 5
- The first Mali is in double quotes so it is a string and the characters in the string will be printed.
What would the following code print?
Mali = 5
print("Mali" + " is " + str(Mali))
Note
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.
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.
Write a Madlib that uses the variables below in a 1-2 sentence story. Make sure the story still works if you change the values of the variables!
There are many possible stories, here is one: