Mad Libs are activities that have a person provide various words, which are then used to complete a short story in unexpected (and hopefully funny) ways.
Complete a program that reads four values from input and stores the values in variables first_name, generic_location, whole_number, and plural_noun. The program then uses the input values to output a short story. The first input statement is provided in the code as an example.
Notes: To test your program in the Develop mode, pre-enter four values (in separate lines) in the input box and click the Run program button. The auto-grader in the Submit mode will test your program with different sets input of values.
Ex: If the input values are:
Eric
Chipotle
12
cars
then the program uses the input values and outputs a story:
Eric went to Chipotle to buy 12 different types of cars
Ex: If the input values are:
Brenda
Philadelphia
6
bells
then the program uses the input values and outputs a story:
Brenda went to Philadelphia to buy 6 different types of bells
481830.3351608.qx3zqy7
LAB ACTIVITY
1.11.1: LAB: Input: Mad Lib
0 / 10
main.py
Load default template…
1
Develop modeSubmit mode
Run your program as often as you’d like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program’s output in the second box.
Enter program input (optional)
Run program
Input (from above)trending_flat
main.py
(Your program)
trending_flatOutput (shown below)
Program errors displayed here
File “main.py”, line 4 using namespace std; ^ SyntaxError: invalid syntax
Coding trail of your workWhat is this?
The Correct Answer and Explanation is :
Here is the completed Python program that implements the Mad Lib functionality as described:
Code
# Read input values from the user
first_name = input() # Example: Eric
generic_location = input() # Example: Chipotle
whole_number = input() # Example: 12
plural_noun = input() # Example: cars
# Output the story using the input values
print(f"{first_name} went to {generic_location} to buy {whole_number} different types of {plural_noun}")
Explanation
- Inputs and Variables:
- The program starts by taking four inputs from the user using the
input()function. These inputs correspond tofirst_name,generic_location,whole_number, andplural_noun. - The
input()function captures the user’s input as a string. Even if the user enters numbers, they are stored as strings.
- Story Formatting:
- The program constructs a short story using an f-string.
- F-strings allow embedding variables directly within a string by placing them in curly braces
{}. For example,{first_name}is replaced with the value of thefirst_namevariable. - The format used is:
"{first_name} went to {generic_location} to buy {whole_number} different types of {plural_noun}".
- Output:
- The
print()function displays the formatted string to the user. It dynamically uses the input values to create a customized story.
- Testing the Code:
- If the inputs are:
Eric Chipotle 12 carsThe output will be:Eric went to Chipotle to buy 12 different types of cars - If the inputs are:
Brenda Philadelphia 6 bells
The output will be:Brenda went to Philadelphia to buy 6 different types of bells
- Why This Approach?:
- Using
input()ensures flexibility to accept any input during testing. - The use of f-strings makes the code concise and readable, which is ideal for formatting dynamic content like this.
This simple program is modular and adaptable to various scenarios where inputs determine the story content.