Poetry generator

Edited

Here’s a project that generates poetry with computer code. Can you figure out how to make it?

viking.gif

Each time you click, you receive a unique poem. Of course, when you code it, you can make your own poem about horses, your family, or nature — or anything else you like.

Wondering how to do it? Here’s a hint: The code uses 5 different list variables to store word banks. Each list contains a different set of words, and each list includes words that are only a single part of speech. For example, there’s a noun list that contains only nouns — words like “dragon” and “sword” and “sea.”

The code picks random elements from the lists, then joins them together. Here’s the code that generates the first line of poetry:

Here’s a trick to make sure each line of poetry appears on its own. The special \n character will add a linebreak (like pressing “return” when you’re composing an email or writing a paper).

This project was inspired by computational poetry projects in Learn to Program with Scratch (2014, No Starch Press) and Learning with LOGO (1983, McGraw-Hill).