Test and Clean Up Your Script
00:00 Okay, that program is all set up and we’re ready for the first proper test run. Going to press F5 and we get a poem. Does it follow the structure? Does it look good?
00:13 An Incredulous Mouse. An incredulous mouse flutters like the glistening sparrow. Curiously, the mouse whispers the sparrow meow within the melancholic elephant.
00:22 Beautiful. All right, so if I run it again, do I get a different one? F5. A Serene Elephant. A serene elephant shimmers within the fragrant mouse.
00:33 Curiously, the elephant gallops, the mouse whispers upon the incredulous gorilla. So this is working, getting different poems every time, and it’s following the structure that we were intending to create.
00:47 So I think this is all good. What do you think? Did you spot any potential problems in this? Because I know there is one, since I may or may not have left it in there on purpose to make a point.
00:59 But for now, let’s be happy because this is actually the poem that we wanted to create. So I will clean this code up a bit before we then look at how we can still make the code a little bit more safe.
01:13 But the function is done. I’m going to get rid of this comment here, and we’ll say three random nouns.
01:29
One random adverb. Two random prepositions. Then we decide whether it is "A"
or "An"
as the article, and then we create the poem.
01:37
Nice. And then it’s always better to actually return your value from a function, than just print it. I don’t want to have side effects, so I’m just going to return poem
and then down here I’m going to call print()
and pass it make_poem()
, which should have the exact same effect as before.
01:55 Only that potentially I could work with the poem string return value if I wanted to for maybe a different project or something like that.
02:06 One last run after we refactoring.
02:10 Let’s see if we get a good one. A glistening mouse gallops, like the melancholic or luxuriously, the mouse meows, the gorilla howls in the serene elephant.
02:18 Things seem to be happening inside of the elephant, which I don’t know if that’s, I guess that’s where most of the space is anyway. Okay. All right, so this is a finished product.
02:32
You can run this script. Every time you run it, you get a different poem. It follows the structure. And you’ve practiced using a bunch of different functions from the random
module and a bunch of different list methods, as well as nested collections here, a tuple with lists nested inside of it, et cetera.
02:52 That’s pretty nice. I want to give you a chance to think about what a potential problem in the script as you have it right now, or as I have it right now, because you probably wrote something else.
03:04 But yeah, think about that. How could this script create a problem?
03:11 If you have an idea, think how you could solve it. And then when you’re done, move on to the next lesson and then we’ll solve it together.
Become a Member to join the conversation.