Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that you know how to use else if statements to provide multiple conditions and multiple outcomes, let's make the random number guessing game more fun.
-
0:00
Let's put the else if clause to use in the random number guessing game that
-
0:04
we've been working on in this stage.
-
0:06
At this point in the game, the player either guesses the correct answer or
-
0:09
not, but we can improve it by giving the player a clue,
-
0:13
then letting the player guess again.
-
0:15
Let's see what the finished project will look like.
-
0:18
When the player fills out the prompt dialogue box,
-
0:20
there are three possible outcomes.
-
0:23
If the guess is equal to the random number, then the player wins and
-
0:26
the program's over.
-
0:28
But if the guess isn't correct, then we'll check to see if the guess is less than
-
0:33
the random number, then we'll tell the player that the random number is larger.
-
0:37
In other words, we'll give them a clue and let the player guess again.
-
0:41
If the player's second guess is correct, then the player wins, otherwise,
-
0:45
the player loses.
-
0:47
However, if the original guess is more than the random number,
-
0:51
then we'll tell the player that the random number is smaller than their guess, and
-
0:55
we'll let them guess again.
-
0:57
Now if the player's second guess is correct, the player wins,
-
1:00
otherwise, the player loses.
-
1:02
You can follow along in work spaces by clicking the launch workspace button
-
1:06
on this page.
-
1:08
We need to perform three tests.
-
1:10
First, does the player's guess match the random number?
-
1:13
If not, is the player's guess less than the random number?
-
1:18
If not, is the player's guess greater than the random number?
-
1:22
I'll start by adding two else if clauses, then I'll add conditions to each.
-
1:32
First, is the guess lower than the random number?
-
1:35
[BLANK_AUDIO]
-
1:38
Next, is the guess higher than the random number?
-
1:41
[BLANK_AUDIO]
-
1:45
Okay, let's deal with this first else if.
-
1:48
If the guess is lower, we'll let the player know and guess again.
-
1:51
[BLANK_AUDIO]
-
1:57
Now we have a new guess from the player provided by a second prompt dialogue.
-
2:02
We need to test to see if the second guess is correct.
-
2:05
We can do this by nesting a conditional statement inside our code block.
-
2:09
[BLANK_AUDIO]
-
2:14
Here's a good example of why indenting your code is important.
-
2:18
This new conditional only runs if the else if clause here is true,
-
2:24
that is, if the player's guess is less than the random number.
-
2:30
This condition tests to see if the second guess is correct.
-
2:34
And if it is, it sets the correct guess variable to true,
-
2:37
because they player got it right.
-
2:39
Now we just need to repeat this for
-
2:41
the case when the player's guess is greater than the random number.
-
2:45
[BLANK_AUDIO]
-
2:52
We can leave the rest of the program the same.
-
2:55
Notice that if the player is correct, we simply change the value of the correct
-
3:00
guess variable to true indicating that the player got the answer right.
-
3:04
That's done here, here and here.
-
3:11
Then in this last if else statement at the end of the program,
-
3:16
we simply test the value of this variable and
-
3:18
print the appropriate message, if the value is true or false.
-
3:23
I'll save the file and preview the WorkSpace.
-
3:29
Now I have a second change to guess if my first guess is wrong.
-
3:35
That's a lot of code.
-
3:36
In fact, this is the largest program we've written so far.
-
3:39
In the next video, you'll learn how to add comments to programs to make it
-
3:43
easier to understand how a program works.
You need to sign up for Treehouse in order to download course files.
Sign up