Skip to main content

Section 15.4 Making a MadLib story

You might have done MadLib stories when you were a kid. You provide some pieces of information, like the 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.

Checkpoint 15.4.1.

    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.
Run this program to see what gets generated, then change some of the variables to make different stories:

Checkpoint 15.4.2.

    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 + "."
    print(start)
    next1 = "A good " + gender + " living at " + address + "."
    print(next1)
    next2 = "One day, a wicked witch came to the " + lastName + " house."
    print(next2)
    next3 = "The wicked witch was planning to " + verb + " " + firstName + "!"
    print(next3)
    ending = "But " + firstName + " was smart and avoided the wicked witch."
    print(ending)
    
  • Yes
  • The only different thing 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.

Checkpoint 15.4.3.

    What would the following code print?
    Mali = 5
    print("Mali" + " is " + str(Mali))
    
  • 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.

Note 15.4.4.

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.

Checkpoint 15.4.5.

Put the blocks below into the correct order to print a twist on a famous poem.

Checkpoint 15.4.6.

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.

Checkpoint 15.4.7.

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!
Answer.
name = "Maria"
month = "October"
place = "Portland"
activity = "sailing"
start = month + " is " + name + "'s favorite month for " + activity + "."
print(start)
end = "This year " + name + " decided that she would go " + activity + " in " + place + "."
print(end)
You have attempted of activities on this page.