codingstreets
Search
Close this search box.

Get Started: Python Mat Libs Generator

Working with Mad Libs Generator is the best way to start your practical Python projects for beginners. This is an excellent task for a newbie in programming. The focus is on variables, strings, and concatenation. This project will show you how to alter inputs from the user. The program’s design requires users to fill in a sequence of inputs, which are thought of as an improbable Mad Lib.

Mad Libs Generator manipulates input data that could be anything from an adjective, pronoun, or verb. After receiving the data, the program analyzes the information and organizes it into an entire story.

Take a look at:

Python Input

Python def

Python while loop

Steps to proceed:

  1. Take input from the user.
  2. concatenate input variable in the print statement.
  3. Execute the print statement but keep adding the previous all print statement in each step.
				
					def write_story():
    print() #for blank-line
    noun1 = input("Enter a noun: ")
    pronoun1 = input("Enter a pronoun: ")
    line1 = "One day, a " + noun1 + " became very hungry as " + pronoun1 + " went to search for some food. "
    print(line1)
    print()

    verb1 = input("Enter a verb: ")
    line2 = "He searched high and low, but couldn’t find something that he could " + verb1 + "."
    print(line1+line2)
    print()

    noun2 = input("Enter a noun: ")
    noun3 = input("Enter a noun: ")
    line3 = "Finally, as his stomach rumbled, he stumbled upon a " + noun2 + " wall. At the top of the wall, he saw the biggest, juiciest " + noun3 + " he’d ever seen."
    print(line1+line2+line3)
    print()

    pronoun2 = input("Enter a pronoun: ")
    adjective1 = input("Enter a adjective: ")
    line4 = pronoun2 + " had a " + adjective1 + " purple color, telling the fox they were ready to be eaten."
    print(line1+line2+line3+line4)
    print()

    verb2 = input("Enter a verb: ")
    verb3 = input("Enter a verb: ")
    line5 = "To" + verb2 + " the grapes, the fox had to " + verb3 + " high in the air."
    print(line1+line2+line3+line4+line5)
    print()

    noun4 = input("Enter a noun: ")
    line6 = "As he jumped, he opened his " + noun4 + " to catch the grapes, but he missed."
    print(line1+line2+line3+line4+line5+line6)
    print()

    noun5 = input("Enter a noun: ")
    line7 = "The fox tried again but missed yet again. He tried a few more times but kept failing. Finally, the fox decided it was time to give up and go " + noun5 + " ."
    print(line1+line2+line3+line4+line5+line6+line7)
    print()

    noun6 = input("Enter a noun: ")
    adjective2 = input("Enter a adjective: ")
    line8 = "While he walked away, he muttered, I’m sure the " + noun6 + " were " + adjective2 + " anyway."
    print(line1+line2+line3+line4+line5+line6+line7+line8)
    print()
    
write_story() #function call

while True:
    ask_user = input("Do you want to write again story? y/n: ")
    if ask_user == 'y':
        write_story()
    elif ask_user == "n":
        print("Had fun!.")
        break
    else:
        print("Invalid")

				
			

Explanation:

First, take input from the user by using the input() function; then add the input variable to the story line and execute the print statement.

Note: Keep adding all previous print statements to the current print statement.

After that, put the whole code inside the def write_story() function; it allows the user to write the story more than once.

Now, ask the user whether they want to write the story again or not by entering the input in “y” or “n” inside the if condition and put this inside the while loop so that the user can write or stop by giving input or breaking the loop by using break keyword.

Here is the complete story –

One day, a fox became very hungry as he went to search for some food. He searched high and low, but couldn’t find something that he could eat.

Finally, as his stomach rumbled, he stumbled upon a farmer’s wall. At the top of the wall, he saw the biggest, juiciest grapes he’d ever seen. They had a rich, purple color, telling the fox they were ready to be eaten.

To reach the grapes, the fox had to jump high in the air. As he jumped, he opened his mouth to catch the grapes, but he missed. The fox tried again but missed yet again.

He tried a few more times but kept failing.

Finally, the fox decided it was time to give up and go home. While he walked away, he muttered, “I’m sure the grapes were sour anyway.”

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on