Lab 4: Mad Libs

Objectives

  • Use gets to obtain user input
  • Use variables to store and manipulate user input

Get Ready to Work

There are no support files needed to complete this lab, as you’ll be writing all the code from scratch yourself. No need for git this time around!

Mad Libs

Remember the game you played in grade school where you come up with words in different categories (e.g., “color”, “person’s name”, “adjective”, “number”), and then your friend reads you a story using those words? If you have, great! If you haven’t — the game’s name is “Mad Libs“, and it’s a harmless bit of fun. Either way, in this lab you’ll be writing a program that lets you play said game with the computer.

When you run your program, it should prompt the player for at least 5 different words (in at least 3 different categories) and then print out a story. You’ll have complete creative freedom over the categories of words and the story.

Here’s what a run through a finished program might look like:

Please enter a person's name:
Michael
Please enter another name:
Jackie
Please enter a color:
blue
Please enter a body part:
nose
Please enter a country:
Thailand
Please enter an adjective:
crazy
Please enter an animal:
cat

Thanks!  Here's your mad lib:

There once was a boy named Michael
who was born, sadly, with a blue nose.
He left his home country of Thailand 
with his crazy, blue-eyed cat 
named Jackie to escape to IIT.

Lab Exercise 1: Mad Lib

Write a program that, as described above, plays a game of Mad Libs with the user. Your program, when run, should prompt the user for at least 5 different words in at least 3 different categories and should subsequently print out a unique story making use of those words.

Each word should be stored in a variable in your program that reflects the category that the input word belongs to. For example, the program that produced the sample output above might contain the variable names name_1, name_2, color, body_part, etc. Save your completed program in a file named “lab4ex1.rb”, and demonstrate it to your TA.

Leave a comment

You must be logged in to post a comment.