Prerequisite: Students should have completed the ‘Answers’ Swift Playgrounds template and have practiced gathering input from a user and inserting the values of variables into a string using ‘string interpolation‘.

Objectives: Practice with Strings, the ideas of input and output, and String interpolation (embedding the values of variables inside a string). Discover that computer programs are seldom written from beginning to end, but instead may be written in reverse order, or most likely, via lots of hopping around.

Introductory Activity: Do a Mad Lib as a class activity. Solicit input for replacement nouns, verbs, adjectives, etc. from a variety of students. Lots of online examples, e.g., http://www.madlibs.com/printables/)

Lesson: Have the students start with the ‘Answers’ template in Swift Playgrounds. Rename the project “Mad Libs”. Students can keep the existing code for reference and add to their Mad Lib program to the bottom of the provided template. Later, when their Mad Libs program is complete, they can clean out the provided show() and ask() templated code.

Explain to the students that to create their own Mad Libs program.

They will need to do the following:

  • Write a short, interesting story (3-5 sentences). Add this as a long Swift string to their program.
  • Decide what important words they would like to be replaceable in their story. They should choose 5-7 words that are a mix of nouns, adjectives, names, locations, verbs, etc. Not all nouns!
  • Replace the key replaceable words with string interpolated variables in their story string. Remind students to use useful variable names. You can provide some helpful code like the following if your students need, e.g.: “The \(noun05) will \(verb01) over the \(adjective03) obstacle.” )
  • At the beginning of the program, students will need to add descriptive prompts using the show() method, and store the inputs the user provides using the ask() method.

They will need to “hold on to” the values that the user enters in separate variables or constants for use later. At the end of the program, when all the input has been collected, the students construct a string by replacing certain words in a story with the alternate values that they gathered from the user. Remind them that they can use String Interpolation to insert values of variables or constants into another string.

Their working Mad Libs program should look something like this when it is running:

Short Discussion: Highlight that this program was created in reverse order. The last part of the program, writing the story with String Interpolation was done first, and the first lines where user input was collected was done last. Make the important point that programs are seldom written in sequential order. It is uncommon for a program to be written starting with the first line, then second, then third, etc. Most of the time, there is a lot of hopping around – sometimes they are written in totally reverse order, like this one.

Concluding Activity:

Ask the students if any would like to share their creation? Showcase several student’s programs having the rest of the class fill in the input nouns, names, verbs, adjectives, etc.

Ask the student “What would you have to do to make the program ask for a story to input also? (This is a much harder problem, as you would have to find a way to denote which words in the initial story should be replaced. Also you would have to know what ‘type’ of word each of the words to be replaced are in order to ask for appropriate replacements. Still, it’s an interesting thought experiment for students and provides extra practice thinking about the all-important question: “What information do I need to keep track of in this program?”

Extension Activity:

Add some scaffolding code to make a voice synthesizer in Swift Playgrounds speak the resulting Mad Lib. See (Lesson: The Name Game) for an example of adding a voice synthesizer to speak a string. Have the student try out different voice dialects (e.g., British, Australian, German, French, etc) from the list below.

Swift Playgrounds Voice Synthesizer Dialect Options