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

Pick a Random Adverb

00:00 Okay, the final random word that you still need to pick is one adverb. So you’re going to work with the adverbs list, which sits at index four of my words tuple, and I just need to pick out one.

00:15 So I’m going to go back to random.choice(). The adverb that I’m using is going to be random .choice() and passing in words at the index four.

00:30 That should be that. Let’s figure out what it does, what we expect it to do. Again, I will save and run this. I get reluctantly. I run it again, I get reluctantly again. I run it again.

00:47 I get reluctantly again. So that’s interesting.

00:51 And there you go, vigorously. Okay, so we just got lucky to get three times reluctantly, but it does a random selection out of the list, and that was just coincidence.

01:02 So this is working. One thing to note here is that I have not added this to a list. So the adverb is different to the other ones in that sense. adjectives is a list, prepositions is a list, nouns is a list, and verbs is a list.

01:16 However, adverb is just a string because I’m just picking out one. So depending on the program that you’re writing, it might make sense that you, even if this is just one element, you might want to wrap it inside of a list.

01:29 You could do that. You could just do it by wrapping the call to random.choice() inside of a list. I’ll just show you that, but I don’t think I’m going to keep it.

01:39 But you can see that now it’s a string inside of a list, like a single-element list.

01:44 Depends really what you’re doing with your code. It could be nice to keep the same structure as the other ones, but here I’m just using it to create this string essentially in the end.

01:53 And I’m just going to insert all of these different words that I randomly picked out anyways, so I’m just going to stick with a string here.

02:02 And yeah, this gives us all the strings.

02:06 Let me clean that up a bit. What did we do here? We picked out one adverb, three

02:14 adjectives, three verbs,

02:20 and three nouns. There we got it. Three nouns, three verbs, three adjectives using list comprehension, two prepositions, and one adverb. Cool. I’m going to keep it a bit cleaner, but that means we should have access to all the words that you are going to need to create the poem following this structure down here.

02:41 So there’s really just two tasks left now. First, we want to decide whether to use a or an for that article. And then we want to create the poem using that structure.

02:51 So only one word to figure out, and that’s just figuring out whether we want to use a or an. And for that, you’re going to have to write some logic.

02:58 We’ll do that in the next lesson.

Become a Member to join the conversation.