Python Beginner Project: Poem Generator

In this project, you will build a program that generates a random poem using a predefined structure and a list of words.

Let’s Build The Project!

Step 1: Import the necessary libraries

The first step is to import the libraries that we will use in our program. We will need the random library to generate random numbers, and we will also need the time library to pause the program for a short time between each line of the poem.

import random
Code language: Python (python)

Step 2: Define the structure of the poem

Next, we will define the structure of the poem by creating a list of strings that represent each line of the poem. The list can be as long as you like, but for this example, we will create a poem with four lines.

poem_structure = [ "The {adjective} {noun} {verb} {adverb}", "Through the {adjective} {noun}", "With a {adjective} {noun} {verb}", "And a {adjective} {noun} {verb} {adverb}" ]
Code language: Python (python)

In each line of the poem, we have placeholders for different types of words: {adjective}, {noun}, {verb}, and {adverb}. We will replace these placeholders with random words from our list of words in the next step.

Step 3: Create a list of words

Now, we will create a list of words that we will use to fill in the placeholders in the poem structure. For this example, we will create four lists of words: one for adjectives, one for nouns, one for verbs, and one for adverbs.

adjectives = ["red", "blue", "green", "yellow", "orange", "purple"] nouns = ["apple", "banana", "grape", "strawberry", "watermelon", "pear"] verbs = ["eat", "drink", "smell", "taste", "see", "touch"] adverbs = ["slowly", "quickly", "happily", "sadly", "gracefully", "carefully"]
Code language: Python (python)

Step 4: Generate the poem

Now, we are ready to generate the poem. We will do this by looping through each line of the poem structure and replacing the placeholders with a random word from the corresponding list of words.

for line in poem_structure: # Replace the placeholders with random words output = line.format(adjective=random.choice(adjectives), noun=random.choice(nouns), verb=random.choice(verbs), adverb=random.choice(adverbs)) # Print the line print(output)
Code language: Python (python)

Step 5: Run the program

Finally, we can run the program to generate a random poem. If you run the program multiple times, you will get a different poem each time because the words are chosen randomly.

if __name__ == "__main__": generate_poem()
Code language: Python (python)

Full Code

Here is the full code for the program:

import random def generate_poem(): # Define the structure of the poem poem_structure = [ "The {adjective} {noun} {verb} {adverb}", "Through the {adjective} {noun}", "With a {adjective} {noun} {verb}", "And a {adjective} {noun} {verb} {adverb}" ] # Create a list of words adjectives = ["red", "blue", "green", "yellow", "orange", "purple"] nouns = ["apple", "banana", "grape", "strawberry", "watermelon", "pear"] verbs = ["eat", "drink", "smell", "taste", "see", "touch"] adverbs = ["slowly", "quickly", "happily", "sadly", "gracefully", "carefully"] # Generate the poem for line in poem_structure: # Replace the placeholders with random words output = line.format(adjective=random.choice(adjectives), noun=random.choice(nouns), verb=random.choice(verbs), adverb=random.choice(adverbs)) # Print the line print(output) if __name__ == "__main__": generate_poem()
Code language: Python (python)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *