Assemble the Words Into a Poem
00:00 You’ve made it all the way here. You got all the words that you need to generate an endless amount of poems. Not quite endless, but there’s a lot of combinations that these amount of words that you have in there could create.
00:12 So now you just need to assemble it in that form. Okay? And I think I’ve mentioned at the beginning that probably using an f-string is a good idea. Create the poem.
00:24
I’ll assign it to a variable that I’ll call poem
and then start off with a string, right? So the first line here, I’m actually just going to copy that because in the way that I named my variables in here, I don’t actually need to change anything here.
00:42
This is just going to insert article
because f-strings work with these curly braces to interpolate values into it. So this is going to be replaced with "A"
or "An"
, depending on what we calculate it should be.
00:55
Here you’re going to change it a bit because I don’t have a variable named adjective
, but I have a list called adjectives
, and I want to get the first one of that list.
01:05
So I will pick out adjectives[0]
, which is the first element in the adjectives
list. And then I can do the same thing over here.
01:13
I want to get the first noun. So I’ll say nouns[0]
. That should give us the title of the poem. Let’s try it out.
01:26
So I’m going to save and run this. And the first title of the poem is A Furry Sparrow
. Try it again. An Exuberant Sparrow
. Look at that sparrow go! An Incredulous Eagle
.
01:38 So this is working, I get different words, and they are assembled in the way that we expect the title of the poem to be.
01:47 But I need multiple lines here. So I could just go with a multi-line string, which might be an easy way to do this. But I want to show you something. Actually, you can use multiple lines to write your strings if you wrap them inside of parentheses.
02:01 Okay, so now I can write another f-string just in the next line.
02:08 This is just going to get stuck together. But Python, basically it’s, it’s going to pretend that this is just one long string, which means that here I’m going to need to add some newline characters.
02:18
And I want two newline characters. Here’s the first one following noun1
, and then there’s another one that just is going to represent an empty line.
02:27 And then I can continue with, well, the start is the same as up here, so I can just go ahead and copy that, and we’ll run it again to see whether we are heading in the right direction here.
02:40
F5 save and run. A Fragrant Badger
. And then the poem starts. Cool. And something that’s interesting with this, using parentheses for strings like that, is you may have noticed that the syntax looks similar to creating a tuple, right?
02:55 And if you actually put a comma here, then you would create a tuple out of two strings instead of concatenating the string together. So this is why the commas are the important part about tuples and not actually the parentheses.
03:10
Let’s continue though. Here I want to continue this one with article
, adjective
, noun
, and then verb
, preposition
.
03:21 I’m going to put this in a new line because Python’s going to stick it together, like I mentioned, and then it’s going to stay easier to read. So this is still line two because I haven’t added a newline character at the end.
03:32
And I’ll just continue going how I was. verbs[0]
and now I want the verb number one, which is my verbs[0]
. preposition[0]
, which is prepositions[0]
.
03:47
Adjective 2 will be adjectives[1]
, at index one, and noun 2 is going to be
03:58
in my nouns[1]
. This should give me the complete first line again. Let’s give it a go.
04:08
A glistening eagle hauls in the
serene elephant
, whatever that means. But one thing that I’ve noticed here is already that, because Python just sticks these strings together, I also need to add a space here at the end for it to read correctly.
04:23
A serene badger
meows for the fragrant mouse
. All right, that sounds like something I even understand.
04:30 Let’s keep going. So we got the first line done, we got the title done, and the first line. Now we’ve got to add a newline character here to end this line and go to the next line.
04:42
And I will use another f-string and replace what’ve we got, adverbs[0]
,
04:51
the noun 1, so this will be, again, nouns[0]
. And verb 2 is verbs[1]
. For me, that’s its line. So I’m going to add a newline character at the end.
05:07 And then we’ve got the last line of the poem,
05:14
which uses noun 2. So I will say nouns[1]
, the second noun. Verb 3 is verbs[2]
.
05:25
Preposition 2 is prepositions
at index 1, and we’re getting close. Adjective number 3, so I will need adjective at index 2 and noun 3. I’m running out of space here. Okay.
05:47
I will after all, create a new f-string here for the last two pieces, adjectives[2]
and nouns[2]
. All right, hope that makes sense.
06:01 And I think if I run that now, I should get the proper poem. Let’s try it out in the next lesson.
Become a Member to join the conversation.