2.8 Write a Mad Libs program - Video Tutorials & Practice Problems
Video duration:
7m
Play a video:
Video transcript
<v ->We've actually learned quite a bit of stuff now</v> and we're ready to move onto our first challenge. We're gonna be writing a little Mad Libs program. If you're not familiar with what Mad Libs is it's like a story or a poem where some of the words are missing and you ask the user to provide those words for you. We can go into challenges and then challenge one Mad Libs. We've got some stuff here already. We've got a variable called story and inside of story is a multi-line string. These three quotes at the beginning and end are defining a string but it's also saying it can include new line characters just by pressing enter. Normally, you would have new line characters in a string by having slash n but this one will print it all out. We can see what the value is, oh, actually it's already printed down there. So, let's just run this, see what happens. So, it just prints out as is including new line characters. The first thing we wanna do is ask the user to provide some of these missing words. We've got two colors and an adjective, so I can say color one or just color equals input, give me a color. You don't have to spell it the Canadian way or I guess British way if you don't want to. And then, we're gonna get another color and save that to color two. And then, lastly, an adjective. So, I'll run this. Mauve, blue, and spicy. So, nothing failed, that's good. Everything printed out the way I think it should. But now we have to insert these here. There's a few different ways of doing this. If you wanted to use the string format way, we can do story and then format and then pass in some values. Normally, format would be used without having these words in here. It would just be, like, using the curly braces and it would just pass them in in the right order. So, we could do this and pass in color two and then adjective. Hey, and that actually works. That's great. If you wanted to keep those words inside of here, we can use format again, passing in keywords instead. Keyword arguments to say that color equals color and adjective equals adjective. This word has to match the word in here, so, maybe we called this color one and then I would pass color one in but it would get renamed to color when it gets passed into the function. Seafoam green and gray and sticky. So, that also worked. Finally, let's look at how to use f strings. So, we can do multiline f strings. We just put an f at the beginning and now these change color and you can see that it's actually trying to accept a variable now but these variables haven't been defined yet so we get name error, color not defined. To change that, all we have to do is move it to the bottom or we just had to ask the user to give input at the top. Now these variables are defined and we can use them in our f string. Purple, burnt umber, and groggy. K, and that worked as well. So, that is our Mad Libs project and you can there are some different ways you could adapt this. One is, of course, you can write your own story, you can, based on what a user inputs, we'll see later on how to do different things using if statements. So you could write your own choose your own adventure game. You can also change it so that if you have a string, so this wouldn't be using f strings, but say you had a string, you can actually find all of these words and extract them and programmatically ask the user for a color and then, color two and adjective. So, there are a few different ways to make this a much more complicated program but here's the base of it. So, that's it for lesson two and we've already covered quite a few of these fundamental programming concepts and looked at how to use them in Python.