1 00:00:00,290 --> 00:00:04,440 Let's put the else if clause to use in the random number guessing game that 2 00:00:04,440 --> 00:00:06,480 we've been working on in this stage. 3 00:00:06,480 --> 00:00:09,920 At this point in the game, the player either guesses the correct answer or 4 00:00:09,920 --> 00:00:13,300 not, but we can improve it by giving the player a clue, 5 00:00:13,300 --> 00:00:15,260 then letting the player guess again. 6 00:00:15,260 --> 00:00:18,010 Let's see what the finished project will look like. 7 00:00:18,010 --> 00:00:20,690 When the player fills out the prompt dialogue box, 8 00:00:20,690 --> 00:00:23,240 there are three possible outcomes. 9 00:00:23,240 --> 00:00:26,920 If the guess is equal to the random number, then the player wins and 10 00:00:26,920 --> 00:00:28,510 the program's over. 11 00:00:28,510 --> 00:00:33,580 But if the guess isn't correct, then we'll check to see if the guess is less than 12 00:00:33,580 --> 00:00:37,610 the random number, then we'll tell the player that the random number is larger. 13 00:00:37,610 --> 00:00:41,550 In other words, we'll give them a clue and let the player guess again. 14 00:00:41,550 --> 00:00:45,360 If the player's second guess is correct, then the player wins, otherwise, 15 00:00:45,360 --> 00:00:47,350 the player loses. 16 00:00:47,350 --> 00:00:51,730 However, if the original guess is more than the random number, 17 00:00:51,730 --> 00:00:55,360 then we'll tell the player that the random number is smaller than their guess, and 18 00:00:55,360 --> 00:00:57,090 we'll let them guess again. 19 00:00:57,090 --> 00:01:00,047 Now if the player's second guess is correct, the player wins, 20 00:01:00,047 --> 00:01:02,480 otherwise, the player loses. 21 00:01:02,480 --> 00:01:06,120 You can follow along in work spaces by clicking the launch workspace button 22 00:01:06,120 --> 00:01:06,740 on this page. 23 00:01:08,280 --> 00:01:10,340 We need to perform three tests. 24 00:01:10,340 --> 00:01:13,670 First, does the player's guess match the random number? 25 00:01:13,670 --> 00:01:18,000 If not, is the player's guess less than the random number? 26 00:01:18,000 --> 00:01:22,270 If not, is the player's guess greater than the random number? 27 00:01:22,270 --> 00:01:31,220 I'll start by adding two else if clauses, then I'll add conditions to each. 28 00:01:32,490 --> 00:01:35,429 First, is the guess lower than the random number? 29 00:01:35,429 --> 00:01:38,834 [BLANK_AUDIO] 30 00:01:38,834 --> 00:01:41,635 Next, is the guess higher than the random number? 31 00:01:41,635 --> 00:01:45,439 [BLANK_AUDIO] 32 00:01:45,439 --> 00:01:48,109 Okay, let's deal with this first else if. 33 00:01:48,109 --> 00:01:51,712 If the guess is lower, we'll let the player know and guess again. 34 00:01:51,712 --> 00:01:57,985 [BLANK_AUDIO] 35 00:01:57,985 --> 00:02:02,790 Now we have a new guess from the player provided by a second prompt dialogue. 36 00:02:02,790 --> 00:02:05,550 We need to test to see if the second guess is correct. 37 00:02:05,550 --> 00:02:09,497 We can do this by nesting a conditional statement inside our code block. 38 00:02:09,497 --> 00:02:14,969 [BLANK_AUDIO] 39 00:02:14,969 --> 00:02:18,740 Here's a good example of why indenting your code is important. 40 00:02:18,740 --> 00:02:23,940 This new conditional only runs if the else if clause here is true, 41 00:02:24,990 --> 00:02:28,480 that is, if the player's guess is less than the random number. 42 00:02:30,610 --> 00:02:34,070 This condition tests to see if the second guess is correct. 43 00:02:34,070 --> 00:02:37,630 And if it is, it sets the correct guess variable to true, 44 00:02:37,630 --> 00:02:38,730 because they player got it right. 45 00:02:39,930 --> 00:02:41,617 Now we just need to repeat this for 46 00:02:41,617 --> 00:02:45,132 the case when the player's guess is greater than the random number. 47 00:02:45,132 --> 00:02:52,840 [BLANK_AUDIO] 48 00:02:52,840 --> 00:02:55,750 We can leave the rest of the program the same. 49 00:02:55,750 --> 00:03:00,460 Notice that if the player is correct, we simply change the value of the correct 50 00:03:00,460 --> 00:03:04,980 guess variable to true indicating that the player got the answer right. 51 00:03:04,980 --> 00:03:11,620 That's done here, here and here. 52 00:03:11,620 --> 00:03:16,000 Then in this last if else statement at the end of the program, 53 00:03:16,000 --> 00:03:18,560 we simply test the value of this variable and 54 00:03:18,560 --> 00:03:23,320 print the appropriate message, if the value is true or false. 55 00:03:23,320 --> 00:03:27,150 I'll save the file and preview the WorkSpace. 56 00:03:29,310 --> 00:03:32,540 Now I have a second change to guess if my first guess is wrong. 57 00:03:35,150 --> 00:03:36,150 That's a lot of code. 58 00:03:36,150 --> 00:03:39,450 In fact, this is the largest program we've written so far. 59 00:03:39,450 --> 00:03:43,160 In the next video, you'll learn how to add comments to programs to make it 60 00:03:43,160 --> 00:03:45,610 easier to understand how a program works.