Pick Three Random Nouns
random.choice() And then I’ll pass in the
nouns list. Because I wrapped this in a tuple, in a
words tuple, I need to first go to the nouns list, and then I need to pass the nouns list in this case.
01:13 So I’ll press F5 to save and run, and I get out elephant. And then if I run it again, I get an eagle. Okay, it looks like what I want. There’s elephant and eagle, so it picks randomly from the nouns list.
01:31 Next on is moss. Cool. I like that. That’s working. I could probably continue working with that. With this, I pick out one word, and then I could just copy that and do it three times and call these noun one, two, and three.
And because this is a list, I can mutate it. I can use the
.remove() method, which is maybe one that you haven’t covered yet, but you pass it a value from the list, and then it’ll remove the first occurrence of that value from the list, and it mutates the list.
So I can pass in the randomly selected noun from the previous line and remove it from the list so that then when I run
random.choice() the second time, it’s not possible that it’s going to pick that again because this is just not part of the list anymore.
Become a Member to join the conversation.