Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Challenge: Poetry Generator

00:00 Are you ready for a challenge? Then it’s time to build your own poetry generator. Start by creating five lists of different word types. That’s about nouns, verbs, adjectives, prepositions and adverbs.

00:14 And here’s some example lists that you can just go ahead and copy/paste if you want, or you can define your own lists using different words. If you want to create your poem, randomly select words from these different lists.

00:30 You want to select three nouns, three verbs, three adjectives, two prepositions and one adverb.

00:38 As an example of how you can do this random selection, you could use the choice() function in the random module that takes a collection as an input and then returns a randomly selected element from there.

00:50 And here’s an example of how it works.

00:53 You’d have to import the random module, and then you can pass, for example, a list to the choice() function. Call that function by saying random.choice() open up the parentheses and then passing in one of the lists that you want to select from, and then closing the parentheses of the function call.

01:11 And that is going to pick randomly one of those three. So either A, B, or C. And every time you run it, it would randomly pick one.

01:21 This is just one way that you can do it. I’m going to encourage you to keep exploring other options too. Take a look at this random module and see what other functions are in there.

01:30 Maybe there’s something else you could use or something that could be helpful. Or maybe there’s also another list method that you could use when you’re building your program.

01:38 So don’t necessarily feel like you’re confined to just using this specific function, but you can do the whole program also with just using this one function, basically.

01:48 I mean, you’ll need data code, but in terms of random selection of elements.

01:55 Okay, so you randomly selected these number of words, and then with those words, you want to generate and display a poem that should follow the structure that you can see below.

02:06 And this is inspired by Clifford Pickover, who seems to be an interesting dude. You can check out the link on the slide here. Click it to his Wikipedia page.

02:15 He did a lot of interesting things that are in the, at the intersection between science, math, and art.

02:21 Okay, so the structure of the poem should be that you start off with an article, then the first adjective that you picked, then the first noun that you picked.

02:30 Then again, an article, again the first adjective, again the first noun. You start off with the title basically, and then the first line of your poem starts with the title as well.

02:42 And then you use the first verb, the first preposition. Then here’s a “the” in there that’s just hard-coded. It’s always going to be a “the”, then another adjective, and then another noun, the second adjective, second noun.

02:53 Then you start the second line off with the first adverb, then comma “the”, then the first noun again, and then the second verb. And then the third line you start off with “the”.

03:06 Then comes again the second noun. Then you use the third verb, the second preposition, another hard-coded “the”, and then the third adjective. And the third noun.

03:20 Finally, here in this schema of how you should create the poem, the letters adj stand for adjective and prep stands for preposition. And also keep in mind that the article that you used twice in here should reflect whether or not the following adjective starts with a vowel or not.

03:39 It’ll have to be “a” if it doesn’t start with a vowel, and it should be “an” if it starts with a vowel.

03:46 So this is the schema. Think about it a bit. And essentially it’s just about inserting the randomly selected words into their respective places. So an f-string is probably a good idea for that.

03:59 Then for the article, you’ll need a bit of extra logic, because that’ll depend on the first letter of the following adjective.

04:08 And then here’s a little example of what kind of poem that the program could then generate.

04:14 So this one says “A melancholic artwork,” and then “A melancholic artwork shimmers like the furry horse. Extravagantly the artwork lops. The horse meows over the incredulous gorilla.”

04:27 So this is a poem that I generated based on the structure that you’ve seen before, and based on this set of words that you can copy that are also in the slide.

04:39 All right? And every time that you run the program, it should generate a new poem. This is just one outcome of running this program. But then if you run it again, you should get a different poem based on the random selection of the words.

04:52 Does that sound intimidating? Or I hope it sounds fun. You’re going to write some code and create some poetry at the same time. I’m excited about this. I hope you are too. Give it a try.

05:01 See what you come up with. If you get a good poem, then post it in the comments, and in the next lesson, I’ll start building out the program, and you can watch me do it and compare your solution to mine.

Become a Member to join the conversation.