1
00:00:04,939 --> 00:00:08,900
So now that we've seen how to use the
debugger to step through our code line by line,

2
00:00:08,900 --> 00:00:11,760
let's explore if statements a bit more.

3
00:00:11,760 --> 00:00:15,180
So I'm going to continue with the
project we set up for this section of

4
00:00:15,180 --> 00:00:21,680
the course - Program Flow. I'm going to
create a new python file and we're going to call that guessing game.

5
00:00:26,000 --> 00:00:29,140
Alright. Now I'm going to start this up very simply

6
00:00:29,140 --> 00:00:32,680
but we'll improve it as we go along.
Firstly, I'm going to start by binding

7
00:00:32,720 --> 00:00:36,560
a variable to the answer. Now the answer
will always be five at the moment

8
00:00:36,560 --> 00:00:42,000
and that's a bit boring but we'll improve
the program later. So answer = 5

9
00:00:42,000 --> 00:00:45,600
So next I want to print a prompt to let
the user know what they're entering,

10
00:00:45,600 --> 00:00:49,670
and then we'll use the input function to get their input. So on line 3 I'm gonna

11
00:00:49,670 --> 00:00:58,360
type print parenthesis double quotes,
Please guess a number between one and ten.

12
00:00:58,360 --> 00:01:03,340
We'll put a colon and a space there, then we've got that closing double quote and right parenthesis.

13
00:01:03,340 --> 00:01:08,180
Then we're going to accept
the input by typing guess equals int

14
00:01:08,180 --> 00:01:13,820
left parentheses input, left and right
parentheses and then a closing right parenthesis.

15
00:01:13,820 --> 00:01:17,240
Now I haven't provided a prompt
this time. We're printing a message on

16
00:01:17,240 --> 00:01:20,520
line three which tells the player what
the program's expecting them to do.

17
00:01:20,520 --> 00:01:24,580
Line four uses the input function without a prompt, but we still need the opening and

18
00:01:24,590 --> 00:01:28,970
closing parentheses after it, as you saw
me type. As before, we're using the int

19
00:01:28,970 --> 00:01:33,380
function to convert their input to an
integer. All input is stored as a string,

20
00:01:33,380 --> 00:01:37,500
but here we want to deal with numbers. So on line 4, we're actually binding the

21
00:01:37,500 --> 00:01:41,460
name guess to the integer value that
we've converted to an integer.

22
00:01:41,460 --> 00:01:44,980
So make sure you only entered valid numbers when
we run the program. We'll be looking at

23
00:01:44,980 --> 00:01:47,640
how to cope with invalid input later in
the course.

24
00:01:47,640 --> 00:01:51,320
Alright though, now it's time to see if
the players guess was correct. Now here,

25
00:01:51,320 --> 00:01:55,160
when we run the program, there's three
possible outcomes. The guess could be too

26
00:01:55,160 --> 00:02:00,110
low, it could be too high or they could
have guessed it correctly. What we want

27
00:02:00,110 --> 00:02:05,000
to do first is test for a too low guess.
We'll do that first and we'll do that on

28
00:02:05,040 --> 00:02:14,440
line 6 by typing if guess is less than
answer : then in parentheses, sorry print, then

29
00:02:14,440 --> 00:02:20,440
parenthesis on line 7, double quotes,
Please guess higher. Then we've got our

30
00:02:20,440 --> 00:02:25,400
closing double quote and right parenthesis. So
basically, if their guess is less than the

31
00:02:25,400 --> 00:02:29,660
answer, we print a message asking them to
guess higher. Now there's a bit more we

32
00:02:29,660 --> 00:02:32,959
need to do here but we'll come back to
that. Before we do though, let's add a

33
00:02:32,960 --> 00:02:37,700
test for the guess being too high. Now
we can do that using elif. So I'm here on

34
00:02:37,740 --> 00:02:43,940
line 8, I'm going to type elif guess.
This time I'm going to type greater than

35
00:02:43,940 --> 00:02:48,100
answer :
Then we're going to print on line 9,

36
00:02:48,100 --> 00:02:54,700
parenthesis double quote, Please guess
lower. Then we've got our closing double

37
00:02:54,700 --> 00:02:59,440
quote and right parenthesis. Now you might
think that we can just use if there, and

38
00:02:59,440 --> 00:03:04,620
you often can, but I'm going to discuss
why that's sometimes not a good idea, shortly.

39
00:03:04,620 --> 00:03:09,139
Alright, so if the condition on
line 6 is false, the condition on line

40
00:03:09,140 --> 00:03:13,400
8 is tested, and if that evaluates to
true, in other words, if their guess is

41
00:03:13,400 --> 00:03:18,100
greater than the answer, then we print
the message Please guess lower. So that's

42
00:03:18,109 --> 00:03:21,799
two of the three possible outcomes dealt
with. We're checking for the guess being

43
00:03:21,800 --> 00:03:26,360
less than the answer, and for it being
greater than the answer, so far. If both

44
00:03:26,400 --> 00:03:30,260
those conditions are false, then the
guess must be equal to the answer.

45
00:03:30,260 --> 00:03:33,640
In other words, they've guessed correctly. So
therefore, we can handle everything else

46
00:03:33,640 --> 00:03:38,560
here using else. So we're going to type
else: on line 10, and print a message,

47
00:03:38,560 --> 00:03:44,820
print parenthesis double quote, You got it
first time, then we've got a closing

48
00:03:44,820 --> 00:03:50,060
double quote and right parenthesis. So
basically, the message on line 11 is only

49
00:03:50,060 --> 00:03:52,909
going to be printed if both the
conditions on line 6 and line 8

50
00:03:52,909 --> 00:03:58,720
evaluate to false. Because else picks up
everything else, it must come after the

51
00:03:58,720 --> 00:04:04,060
if and elif blocks. elif must follow an if,
and all elif blocks have to appear

52
00:04:04,060 --> 00:04:08,000
before the else block, if there is one.
Right, so let's run the program a few

53
00:04:08,000 --> 00:04:13,120
times to check that it's working. I'm
gonna run it now. Now first I'm gonna

54
00:04:13,129 --> 00:04:17,959
enter my guess as 4. Remember to click
into the run pane, as I just did, before

55
00:04:17,959 --> 00:04:22,520
typing your guess. We get the message "Please 
guess higher", which is correct because

56
00:04:22,520 --> 00:04:27,540
obviously 4 is less than 5. I'm gonna
run it again,

57
00:04:27,540 --> 00:04:32,680
and this time I'm gonna type a higher value, 8.
You can see we've got the output "Please

58
00:04:32,680 --> 00:04:37,600
guess lower", which is also correct. 8, of
course, is more or higher than 5.

59
00:04:37,600 --> 00:04:41,380
The third test is when the guess is correct,
so I'm going to run that again,

60
00:04:41,380 --> 00:04:46,140
and this time I'm going to type in the value 5,
and we've got the message "You got it

61
00:04:46,140 --> 00:04:50,820
first time" on screen. So run the program
a few times with different guesses to

62
00:04:50,820 --> 00:04:54,220
make sure it works, and in the next video
what we'll do is, we'll run the program

63
00:04:54,220 --> 00:04:59,670
in the debugger to check what it's doing.
See you in the next video.

