Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Project 1: Madlibs

Welcome to the first project! This project is a simple project focused on the game Madlibs.

With Madlibs, the rules are simple. A story is created with a number of empty spaces, prompted by the part of speech required. An example story is listed below:

Example Story with the Placeholders

{name} traveled to {place} at {time}. It took {name} {amount of time} to get there. At {place}, {name} tried to eat some {food}. Unfortunately, {pronoun} got sick and ended up throwing up. {second name} came to help clean up.

The example above has the placeholders surrounded by curly-brackets. Let’s say the following values are used:

name = "John"

place = "the park"

time = "12PM on Sunday"

amount of time = "2 hours"

food = "sushi"

pronoun = "he"

second name = "Steve"

If we plug in the values into the placeholders, our story becomes:

John traveled to the at 12PM on Sunday. It took John 2 hours to get there. At the park, John tried to eat some sushi. Unfortunately, he got sick and ended up throwing up. Steve came to help clean up.

While this story isn’t all that long, we can always make it longer. The task at hand will be to create your own story with placeholder values, which our variables will fill in to.

Task

We will create a basic story together with our code. After this section, please add more to the story or change the story to make it your own.

Instructions

  1. Create a variable at the top called name and ask the user to input their name.
  2. Create another variable after the name variable called place and ask the user to input a place.
  3. Next, create a variable named time and ask the user to input a time. Now that we have our first three variables, let’s use them in a story!
  4. Create a string variable called story and set it to an empty f-string.
  5. Within the f-string, we want to create the story. This will look like the following code chunk:
story = f"{name} went to {place} at {time}."

This isn’t a story yet, this is just a sentence. Let’s add another sentence to this story!

  1. Continuing from the story, let’s return above the story variable and add more inputs.
  2. After the time variable, let’s add a variable called duration and ask the user to input how long it took to go to the place.
  3. Create another variable called food and ask the user to input a food.
  4. Next up, create another variable called pronoun and ask the user to input the pronoun of the person.
  5. Lastly, we want another name, so let’s make a final variable named second_name and prompt the user to input another name.
  6. After this, we will update our story to include these new variables. This can look like what’s below!
story = f"{name} went to {place} at {time}. It took {name} {duration} to get there. {pronoun} went to {place} since {second_name}'s birthday was coming up. They wanted to eat some {food} together."
  1. Now, we want to print out the story to see it!
  2. Add a print(story) at the bottom to see the story!

So far, this is a lot of inputs and reading them in a string. That’s not everything we discussed in this chapter, so let’s add more to utilize the concepts of casting and mathematical operators. Go back to the sections of your code around inputs and add the following variables.

  1. Add another variable called age. Ask the user to enter the age of the second person, since we’re going to compute how old they will become. This will start as a string, but we’ll cast this into an integer.
  2. Next up, create another variable called quantity, storing the response from the user after being asked to enter the amount of the previously mentioned food was bought.
  3. Now, we have a quantity, let’s ask the user for the total cost. This should become a floating point value, since monetary values can have decimals.
  4. The last input will be the original budget for both of them. This should require the user to enter how much they both had as their original budget.

Now, after the variables, let’s update our story to include how old the second person is turning, along with how much food they bought, the cost of the food, and how much money they had after they spent their money. Your string should/could look like this:

story = f"{name} went to {place} at {time}. It took {name} {duration} to get there. {pronoun} went to {place} since {second_name}'s birthday was coming up. They wanted to eat some {food} together. {second_name} is turning {age+1} years old! They bought {quantity} of {food}, which cost them ${cost}. They had a collective budget of ${budget}, which left them with ${budget-cost} afterwards."

Suggested additions

While these are simply suggestions, you may iterate upon the story and add your own changes as you see fit. Make this project your own!

  1. Add a third person to the story
  2. Replace the cost input with a calculation. Ask the user how much a single piece of the specified food cost and calculate the cost by multiplying the cost for an individual item by the quantity.
  3. Take in more food and calculate the total cost of all of the food

Solutions

If you’re really unsure how to go about this, there are solutions in the Exercise Solutions section.

Moving forward

Great job working on this project. While it isn’t a huge project, it provides a chance to use the various aspects covered in this chapter! If you’re ready to move forward, please head over to the second chapter Conditional Statements