1
00:00:04,700 --> 00:00:09,020
Alright, so let's revise our guessing
game. specifically, what we're going to do

2
00:00:09,020 --> 00:00:13,720
is give our players another guess, and
we'll do that by requesting input again.

3
00:00:13,720 --> 00:00:17,960
So at the moment, we've got the code on
line 6, which tests whether the guess is

4
00:00:17,970 --> 00:00:20,910
less than answer then we print a message, "Please guess higher". We're going to add

5
00:00:20,910 --> 00:00:25,680
some code down here on line 8, and
we're going to type guess equals

6
00:00:25,680 --> 00:00:30,660
int parenthesis input left and right
parenthesis and then a closing

7
00:00:30,660 --> 00:00:35,129
right parenthesis. Now it's important
that this line is indented at the same

8
00:00:35,129 --> 00:00:39,149
level as line 7, and in my case, you
can see that it is. Both lines are part

9
00:00:39,149 --> 00:00:43,739
of the same code block and are indented
to the same level. Alright, so at this

10
00:00:43,740 --> 00:00:48,000
point we've got a second guess from the
player. The next step is to see if that

11
00:00:48,000 --> 00:00:52,280
guess was correct or not. So we're going
to add some code on line 9 now:

12
00:00:52,280 --> 00:00:58,360
if guess is equal to answer, so two equal
signs there, :

13
00:00:58,360 --> 00:01:06,740
then on the next line, print parenthesis
double quotes Well done, you guessed it.

14
00:01:06,740 --> 00:01:10,000
And obviously, we've going a closing
double quote and right parenthesis there.

15
00:01:10,000 --> 00:01:13,860
So we're comparing guess to answer, to see
if they got the correct answer. So note

16
00:01:13,860 --> 00:01:18,360
that we have to use two equal symbols
here, one on line 8 and one on line 9.

17
00:01:18,360 --> 00:01:22,380
A single equal is used when
binding a variable to a value, or when

18
00:01:22,380 --> 00:01:26,020
assigning a value to a variable, if you
prefer to think of it that way.

19
00:01:26,020 --> 00:01:30,640
Now when testing for equality, we use two
equal signs. Okay, so that's all we need

20
00:01:30,660 --> 00:01:34,220
to do. We've used an if statement to
check if they got the right answer.

21
00:01:34,220 --> 00:01:37,320
I'll leave the code like that because I want
to make it clear that you don't have to

22
00:01:37,320 --> 00:01:41,700
have elif and else in your if statements.
The code would benefit from an else

23
00:01:41,700 --> 00:01:45,720
clause, but we'll come back to that. Al
right, so that's the second guess when

24
00:01:45,720 --> 00:01:50,260
the first guess was lower than the answer.
We'll do the same thing if it was higher.

25
00:01:50,300 --> 00:01:55,360
So I'm going to come down, specifically,
for the code here, and on line 13,

26
00:01:55,360 --> 00:02:00,060
type the same code in. I could copy and paste it.
I'll type it in again, guess equals int

27
00:02:00,060 --> 00:02:04,800
left parenthesis input, left and right
parentheses and then closing parenthesis.

28
00:02:04,800 --> 00:02:11,400
If, on the next line, guess is equal to, so
two equal signs, answer : press ENTER,

29
00:02:11,400 --> 00:02:17,960
print parentheses double quotes, Well
done, you guessed it.

30
00:02:17,960 --> 00:02:21,020
Alright, so we've done the same, when
testing whether the guess is greater

31
00:02:21,020 --> 00:02:25,760
than the answer as well. And again, very
important that you've got the correct

32
00:02:25,760 --> 00:02:28,850
indentation there, and that the code's
indented nicely under the print

33
00:02:28,850 --> 00:02:31,720
statement on line 12, which we've
done.

34
00:02:31,720 --> 00:02:37,340
So our if and elif blocks each contain,
now, another if block. Specifically,

35
00:02:37,340 --> 00:02:40,760
that's the blocks on lines 9 to 10, and
also on lines

36
00:02:40,760 --> 00:02:45,140
14 to 15. The two if
statements are indented one level, at the

37
00:02:45,140 --> 00:02:50,300
same level as the other lines in the same block, as you can see. Lines 10 and 15,

38
00:02:50,300 --> 00:02:53,920
they're indented another level
because they're in another block.

39
00:02:53,920 --> 00:02:59,180
Those lines will only be executed when the condition guess is equal to answer,  is true.

40
00:02:59,180 --> 00:03:06,280
Alright, so let's see if this works. I'm just going to run this as opposed to debugging.

41
00:03:06,280 --> 00:03:10,640
I'm gonna start by
entering 9 as the first guess,

42
00:03:10,640 --> 00:03:14,980
Please guess lower - you can see on the
screen. Now there's quite a few tests to do

43
00:03:14,980 --> 00:03:18,320
here, to check every possible path
through the code. So let's guess

44
00:03:18,320 --> 00:03:23,440
correctly this time. I'm going to enter
5 as the second guess.

45
00:03:23,440 --> 00:03:28,940
We get the message, Well done, you guessed it, printed by line 15. So if you're not sure why the

46
00:03:28,940 --> 00:03:32,870
message came from line 15 rather than
line 10, then run the program in the

47
00:03:32,870 --> 00:03:41,420
debugger to check that. I'm going to run
the program again, this time I'm gonna enter 1.

48
00:03:41,420 --> 00:03:44,300
So you can see we've got the
message telling us to guess higher,

49
00:03:44,300 --> 00:03:50,200
so I'm going to enter 5 - Well done, you guessed
it, on the screen, and this output came

50
00:03:50,200 --> 00:03:54,220
from line 10. And again, if you're not
sure why that's the case, make sure you

51
00:03:54,220 --> 00:03:57,280
run the code through the debugger, step
through the code, and you'll see that

52
00:03:57,290 --> 00:04:00,760
that's the case. Alright, the code's
working when we guess correctly the

53
00:04:00,760 --> 00:04:06,700
second time, but what happens if we get
the second guess wrong. Well let's test that.

54
00:04:06,700 --> 00:04:12,580
So I'm going to enter 1 again, and now
I'm going to enter 9,

55
00:04:12,580 --> 00:04:17,800
and as you see, that's not really very helpful. The program's terminated without telling us what happened.

56
00:04:17,800 --> 00:04:21,519
I wanted to show that you
don't have to use elif and else clauses,

57
00:04:21,519 --> 00:04:25,240
but we really should use else here, to
print out a message when the second

58
00:04:25,250 --> 00:04:28,340
guess is wrong.
So let's actually add the code. so I'm

59
00:04:28,340 --> 00:04:33,040
going to come up to do that on,
after line 10. So I'm going to come down here.

60
00:04:33,040 --> 00:04:39,500
I'm gonna press backspace so that my else is
on the same level as the if, else : enter

61
00:04:39,500 --> 00:04:48,520
print on the next line, parentheses,
Sorry, you have not guessed correctly.

62
00:04:48,520 --> 00:04:53,220
And we want to do the same thing when the
guess is too low, and that's down here on

63
00:04:53,220 --> 00:04:57,240
line 18, which it will be when I type it.
I'm back spacing again to make sure the

64
00:04:57,240 --> 00:05:02,680
else is at the same indentation level as
the if, else, print parentheses double quotes.

65
00:05:02,680 --> 00:05:05,940
And of course, that should be "You
have not guessed correctly", not "You have guessed correctly"

66
00:05:05,940 --> 00:05:09,200
because of course that
means the else is executed if the guess

67
00:05:09,240 --> 00:05:23,580
has not been entered correctly. Same down here, so "Sorry, you have not guessed correctly".

68
00:05:23,580 --> 00:05:28,050
Okay, we got it right in the end. Alright,
so again I mentioned it but pay careful

69
00:05:28,050 --> 00:05:32,160
attention here to the indentation. The if
and else keywords have to be indented to

70
00:05:32,160 --> 00:05:36,870
the same level. This now makes it very
easy to see which else or elif goes with

71
00:05:36,870 --> 00:05:41,100
which if. The indentation is essential
for Python to understand what we want it

72
00:05:41,100 --> 00:05:45,740
to do, but the added benefit is that it
also makes the code easier for us to read.

73
00:05:45,740 --> 00:05:50,080
Alright, so we're going to run the
program again now.

74
00:05:50,080 --> 00:05:54,480
I'm just going to do the same thing. In my case, I'm going to enter 1 as the first guess,

75
00:05:54,480 --> 00:05:59,840
9 as the second one, and this time we've got a message, Sorry, you have not guessed correctly.

76
00:05:59,840 --> 00:06:02,520
So run the program yourself
and experiment with different values

77
00:06:02,520 --> 00:06:06,600
for your guesses. The answer's always 5,
which is a bit boring, but has the

78
00:06:06,600 --> 00:06:10,260
advantage to make it easier to test the
program, in other words, you know which

79
00:06:10,260 --> 00:06:16,040
messages to expect. Alright, so in the
next video we'll look at more of the conditional operators.

80
00:06:16,040 --> 00:06:19,080
See you in the next video.

