Overview

In this lesson, students are introduced to the "text" data type (sometimes called a string in other programming languages) as a way of representing arbitrary sequences of ASCII characters. They will use the "text" data type to accept input from a user as they work on mastering two new User Input (UI) elements, the text input and the text output. Students combine these skills to develop a simple Mad LibsĀ® app using the Online IDE.

*Mad LibsĀ® is a trademark of the Penguin Group (USA) LLC., which does not sponsor, authorize or endorse this site.

Vocabulary

Goals

Students will be able to:

Purpose

Text data types (commonly called "strings" in other programming languages) are a feature of essentially every programming language, and they allow for variable-length pieces of text to be represented, stored, and manipulated. While a single string can be stored in a variable, it is worth noting that a string will typically use much more memory than a number. Numbers are typically stored in fixed-width 8-, 16-, 32-, or 64-bit chunks. ASCII characters require a single byte and so a text value of 100 characters, short by most standards, would require 800 bits in order to be stored in memory.

Resources

Getting Started

Distribute: the Mad Libs - Activity Guide from the Resources section. Students should use this opportunity to decide on what the theme of their Mad Libs app will be, what text they will accept into their app, and how it will be incorporated into its output. The primary guidelines of the project (also included in the Activity Guide) are:

Before moving on to begin coding their Mad Libs app, students should have a rough outline of their project. Once they have completed their outlines, students should begin coding their app.

Activity

Remind students that "Text" is a sequence of ASCII characters contained within double quotes. This sequence of characters can contain numbers, special characters, whitespace, and/or text. As a class discuss and go over a few examples of values that can be stored in a text type variable.

Teacher's Tip

The most important rule of the text variable is that the text value - assigned to the text variable - MUST always be within "Double Quotes"

Here are a few more example outlines that you can have students refer to if they are having trouble coming up with an outline for their Mad Libs.

Pattern 1: A Letter from Camp

Dear [name of a person],

Today is the first day of our camp at [name of a planet]. Things are going really well
so far with minor accidents here and there. We set up our tent on [a place in nature 
that is usually dangerous]. When we were setting the tent poles, [name of a person] 
accidentally hit [name of second person]'s [a part of the body]. Good thing we had [a 
name of an animal] standing near by to take care of the small injury, otherwise [name
of second person again] would have lost an entire [a part of the body] by the time we come home.
Oh, looks like I am needed again!

Love you, [a name of an animal]

Pattern 2: How to train a dog to sit

It could be a challenge to train a dog to "sit."

1. First, you need to know what [dog name]'s favorite food is.
2. Once you have found [same dog name]'s favorite food, hold [type of food]
   in front of his/her [a body part].
3. Since [name of your dog] is a [name of an animal], you need to be careful not to get 
   [verb - past tense] by [name of your dog].
4. Tell [name of your dog] to "sit," and at the same time, gently push down on their [a body 
   part of a dog] with your open hand.
5. Release your hand pressure to the [a body part of a dog], and keep telling [name of 
   your dog] "sit."
6. Once [name of your dog] knows how to keep [a body part of a dog] down with 
   your command of "sit," give him/her [name of a food] for the reward!

Student Instructions

Building this Mad Libs Program is relatively easy for you by now because we have done something like this in Lesson 4 already - Getting the user input for the calculator program. For this project, we will not have to "cast" the user input to an integer, instead we will save the user input as a text type variable to use when displaying the Mad Libs paragraph at the end of the program. Use your brainstorming ideas from the Activity Guide as a template for your MadLibs input and output.

Program Flow

Once completed, your program should:

  1. Prompt the user for text input of different types (e.g., noun, verb, adjective, etc.) - Request at least 3 separate words from the user.
  2. Modify the user's "word" if necessary (e.g. capitalizing proper nouns).
  3. Place the user's input words within your Mad Libs outline using concatenation. - Using meaningful variable names for your user input will help to make this step easier.
  4. Output the Mad Libs to the screen. - Don't forget to add the "say" command to make your Mad Libs app user friendly and accessible.

The example below shows the review of input syntax, concatenation, and how to use the "say" command.

text userInput1 = input("Type in a noun.")
text userInput2 = input("Type in a verb.")

output "My output is " + userInput1 + " " + userInput2
say "My output is " + userInput1 + " " + userInput2

You can write your program without "modifying the user text input" for now. Try running your code after properly concatenating the user input words within your Mad Libs outline.

Writing MadLibs Program

*** Run, Test, Debug!

Modifying the user input

You may need to modify some of the user input to fit your MadLibs outline, such as capitalizing the word at the beginning of a sentence. Remember text type values are just a sequence of ASCII characters. These ASCII characters can be accessed and modified individually to format your text variable to fit your MadLibs outline. The code block below shows an example of the process:

//Creates a text type variable "firstLetter" which will store the ASCII character
//in position 0 of our text type variable "verb".
text firstLetter = verb:GetCharacter(0)
//Converts the ASCII character stored in the "firstLetter" variable to upper case
//(if the character is already upper case then it will remain upper case).
firstLetter = firstLetter:ToUpperCase()
//Creates a text type variable "remainingLetters" which will store the remaining ASCII
//characters (from position 1 until the end of the text) of our text type variable "verb".
text remainingLetters = verb:GetSubtext(1)
//Converts the remaining ASCII characters stored in "remainingLetters" to lower case
//(if the characters are already lower case then they will remain lower case).
remainingLetters = remainingLetters:ToLowerCase()
//Saves our formatted ASCII characters to our text type variable "verb" which we can use to output to the screen
verb = firstLetter + remainingLetters

Add your own code block to your program that will modify the user's input text for proper capitalization in your MadLibs outline.

Modifying User Input

*** Run, Test, Debug!

Make a Prediction: Follow the lines of code below to determine what the output for this code segement would be:

text phrase = "I am so"
text emotion = "excited for"
text place = "school"
text sentence = phrase + " " + emotion + " " + place + " today!"
output sentence

Wrap Up

Once students have completed their applications they should share their work with their peers, trying one another's Mad Libs.

Standards Alignment