How I Made a Poem Generator

Table of contents

No heading

No headings in the article.

I made a Poem Generator. And I am going to teach you how you can too. This is quite simple, but the more phrases you add, the more complex it becomes.
Let's get started.

import random

The import random is for one thing: Chooses randomness.
Yes, it is that simple. Now, because that was a very simple explanation, let's get to the next part, defining phrases and endings:

phrases = [
          "Phrase here"
          ]
endings = [
          "Ending here"
          ]

The endings and phrases save a library of things to say. This allows for randomness, besides import random being the real randomness.

def generate_poem():
    chosen_phrases = random.sample(phrases, 8)
    ending = random.choice(endings)
    poem = "\n".join(chosen_phrases) + "\n" + ending
    print(poem)
generate_poem()

def generate_poem(): defines the poem function, or what you see at the bottom, generate_poem() is the one to execute it.
chosen_phrases = random.sample(phrases, 8) The 8 at the end is what makes it only 8 paragraphs.
ending = random.choice(endings) writes the ending.

And, as always, here is the full code:

import random
phrases = [
          "Phrase here"
          ]
endings = [
          "Ending here"
          ]
def generate_poem():
    chosen_phrases = random.sample(phrases, 8)
    ending = random.choice(endings)
    poem = "\n".join(chosen_phrases) + "\n" + ending
    print(poem)
generate_poem()

So, in general, this is a short and simple poem writer. You can make it more complex if necessary.