Heads up! To view this whole video, sign in with your Treehouse account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Use what you have learned so far to create a Mad Lib console game.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
To put all of your knowledge together,
we're going to create a Mad Lib.
0:00
A Mad Lib is a fun activity
where you ask someone for
0:05
nouns, verbs, adjectives, etc, and
then use them in a silly paragraph.
0:08
We will be working in a new workspace.
0:17
So go ahead and
0:19
launch the latest workspace with
this video if you haven't already.
0:21
Click on the madlib.py file to open it,
0:26
if it isn't open already, and
let's look at our Mad Lib.
0:29
These are called comments.
0:35
They don't run when the file is run in the
console, they're just information for us.
0:37
Each bracket is where we'll need to ask
the user for something, an adjective, and
0:43
then print it out in the sentence.
0:48
Let's create all of the variables and
inputs first.
0:50
I'm going to use the name of the item and
0:55
then a number to represent
its place in the paragraph.
0:57
For instance,
the first one will be called adjective1.
1:01
Then plural_noun2,
1:08
and so on and so forth.
1:12
adjective1 =
1:17
input('Adjective: ').
1:21
plural_noun = input
1:28
('Plural Noun: ').
1:33
verb3 = input
1:39
('Verb: ').
1:44
verb4 = input ('Verb: ').
1:50
adjective5 = input
1:58
('Adjective: ').
2:02
Make sure I got them all.
2:07
Adjective, plural noun,
verb, verb, adjective.
2:15
Next is family member.
2:19
Family_member6 = input
('Family Member: ').
2:28
Need another verb7 =
2:37
input('Verb: ').
2:42
Then we need a plural_noun8
2:46
= input ('Plural Noun: ').
2:52
And finally, we need one more
2:59
adjective, adjective9 =
3:04
input('Adjective: ').
3:09
Awesome.
3:18
We've got all of our inputs written.
3:21
Let's save and run the file.
3:23
This way we can make sure we
don't have any spelling errors.
3:30
Great, next we need to use
the input to print out our Mad Lib.
3:55
Let's print out each
sentence line by line,
4:01
so we can make sure to
use the right variable.
4:06
Create a little bit of space, print and
4:10
we know we're gonna have to use
our string formatting, so f(' ').
4:14
And we can copy and
paste our first sentence here,
4:20
Ctrl + C or Command + C, and then Ctrl or
4:25
Command + V to paste it in here.
4:29
Then we turn our square brackets into
4:33
curly brackets and add our number, 1.
4:38
And let's repeat that for
4:46
all of our sentences print(f' ''),
4:50
copy sentence two, copy, paste,
4:55
and don't forget we'll need
5:00
an underscore here, this will be 2.
5:04
And we'll need to do the same
thing with this verb.
5:11
It could be 3 and
5:15
then print(f' ').
5:18
Let's get our next sentence.
5:24
Change these to curly brackets.
5:35
We're on 4, So
5:39
adjective is 5, curly bracket.
5:45
Repeat again, print(f' '),
5:50
grab our second to last sentence,
5:55
paste, curly bracket, underscore,
6:00
the number 6, curly bracket,
6:07
curly bracket, 7, curly bracket,
6:12
curly bracket, underscore, 8 and
6:18
curly bracket and we have one more.
6:24
Print(f' ')
6:33
Copy and paste,
6:39
curly, 9, curly and
6:43
save, run the file.
6:49
Enter whatever you want.
7:04
Oops, says our family_member6
7:24
is not defined.
7:31
Let's see why.
7:36
Oops, l spelled it wrong.
7:38
l have family memeber.
7:38
So remove that, and then make sure
it's also spelled correctly down here,
7:41
family_member6, okay.
7:46
Save.
7:48
Let's run
7:56
that again.
8:02
And there's your Mad Lib.
8:17
Nice work, you have a working Mad Lib
program, pat yourself on the back.
8:19
Feel free to grab a friend,
roommate or significant other and
8:25
have them input the adjectives and
nouns and run their own Mad Lib.
8:29
As a challenge, I've placed another
Mad Lib and the second file here.
8:35
Go ahead and try creating this
Mad Lib program on your own.
8:41
Pause me and
I'll see you back here in a bit.
8:46
All right,
let me walk you through what I did.
8:50
First, I created all of the variables and
inputs like this one.
8:53
Then I created the print statements with
string formatting using curly brackets and
9:02
the variable names to input
our user's information.
9:08
Save and run the file.
9:12
Make sure
9:21
to put
9:29
madlib2.
9:34
I like to have cats, plants,
9:53
and dogs on my desk at all times.
9:58
This helps me stay focused when I eat.
10:03
I also keep my bug close to keep cars
from bothering me while I catch.
10:06
When I'm running,
I use the Doug Technique.
10:11
It's where you spend 30 minutes gaming and
then 2 minutes sleeping.
10:14
This makes me a wonderful worker.
10:18
I wish I could spend 30 minutes gaming and
10:20
then 2 minutes sleeping to
complete my work every day.
10:22
Take some time and look at all of
the code you've written so far.
10:26
You are on your way to
becoming a great Pythonista.
10:36
So far, you've learned how to create
a variable that holds a string,
10:42
how to print out strings and variables
to the console, how to run a file,
10:47
how to ask for input and save the result
in a variable and how to format strings.
10:53
That's a ton of knowledge already.
11:01
You're amazing.
11:03
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up