1
00:00:04,160 --> 00:00:08,610
Alright so why are we getting these
errors that showed up at the end of the

2
00:00:08,610 --> 00:00:13,320
last video? Well we've got errors because
you must have valid code inside each of

3
00:00:13,320 --> 00:00:17,670
the code blocks. A comment doesn't count.
Notice that there's a red tick in the

4
00:00:17,670 --> 00:00:24,060
right margin next to lines 15 and 18, and
I can select them up here, and also there's this

5
00:00:24,060 --> 00:00:29,160
red squiggle under each comment on
line 15 and 18. Now this can be confusing

6
00:00:29,160 --> 00:00:32,189
when you're starting but it's the syntax
rules of Python.

7
00:00:32,189 --> 00:00:37,200
You must have code inside a code block.
If there's no code, and that includes

8
00:00:37,200 --> 00:00:41,070
just having a comment, then you'll get an
error like this. So notice that there

9
00:00:41,070 --> 00:00:45,870
isn't an error on line 21 or line 24.
There is code inside those two blocks,

10
00:00:45,870 --> 00:00:49,890
so we don't get that error. If getting an
error wasn't confusing enough though, the

11
00:00:49,890 --> 00:00:54,149
error message makes it even worse. And if I
hover over one of them again, you can see the

12
00:00:54,149 --> 00:00:57,600
error we're getting there is: indent
expected. But of course, looking at that

13
00:00:57,600 --> 00:01:01,060
you can see those comments are already
indented - they're obviously indented.

14
00:01:01,060 --> 00:01:04,700
So we can see that on the screen. You may
not see an error if you're using a

15
00:01:04,709 --> 00:01:09,270
different IDE, but you will get it when
you try to run your code. So things like

16
00:01:09,270 --> 00:01:13,920
this can have you tearing your hair out.
Fortunately, there are very few times

17
00:01:13,920 --> 00:01:18,040
when something like this happens, and now
that I've pointed it out, you'll remember

18
00:01:18,040 --> 00:01:22,780
it and avoid tearing your hair out.
What you'll often find programmers doing,

19
00:01:22,780 --> 00:01:28,000
is adding a dummy bit of code inside the
indented block, to clear this error.

20
00:01:28,000 --> 00:01:32,960
So there's a specific keyword in Python for that
and that's called pass. So I'm going to

21
00:01:32,960 --> 00:01:41,340
come up here to line 16, tab over and
type pass. So come down to line 19,

22
00:01:41,340 --> 00:01:47,340
tab over and type pass again. Now pass
doesn't do anything, but it makes the

23
00:01:47,340 --> 00:01:52,760
code syntactically correct. So think of
it as a placeholder for the real code

24
00:01:52,760 --> 00:01:56,580
that you'll add later, and you can see
that that's fixed the errors and we've

25
00:01:56,580 --> 00:02:01,140
now got the basic structure of our code.
Alright so it's time to work out what

26
00:02:01,140 --> 00:02:05,640
we really want to do in place of those
pass statements. If the computer had to

27
00:02:05,640 --> 00:02:10,049
guess higher, well then we alter the
low value of the range and go around the

28
00:02:10,049 --> 00:02:16,310
loop again. So let's do that. So I'm going
to replace that pass on line 16 with low

29
00:02:16,310 --> 00:02:21,660
equals guess plus 1,  which is in
line with the comment that I've

30
00:02:21,660 --> 00:02:27,420
added on line 15. Now similarly, if we have
to guess lower, then the new high value is

31
00:02:27,420 --> 00:02:33,040
one less than the current guess. So
come out to line 19, replace the pass,

32
00:02:33,040 --> 00:02:39,870
and it's going to be high equals guess
minus 1. Alright so the last step now

33
00:02:39,870 --> 00:02:44,460
in our algorithm, is to break out of the
loop when the guess is correct. So we're

34
00:02:44,460 --> 00:02:49,800
going to come down here. Then on line 22,
we're going to add the keyword break

35
00:02:49,800 --> 00:02:53,040
so that we do just that. We break out
of the while loop if the computer has

36
00:02:53,040 --> 00:02:56,760
successfully guessed it. If you've
programmed in other languages, you might

37
00:02:56,760 --> 00:03:00,900
be wondering if we could use a case or
switch there. Don't bother searching for

38
00:03:00,900 --> 00:03:05,840
how to do that in Python. Python doesn't
have a case or switch statement. If you

39
00:03:05,840 --> 00:03:08,620
haven't programmed before, that won't mean
much to you, don't worry about it.

40
00:03:08,620 --> 00:03:12,959
They don't exist in Python. Alright, so
that's our algorithm implemented with

41
00:03:12,959 --> 00:03:17,430
Python code. So we're doing a bit more.
We're also counting the number of

42
00:03:17,430 --> 00:03:21,240
guesses. So that means therefore we
should increase the count before the

43
00:03:21,240 --> 00:03:25,350
loop goes around again. So we need to
come down and add that code, and we

44
00:03:25,350 --> 00:03:28,350
both need to add it at the same
 level as the if, and I'm gonna

45
00:03:28,350 --> 00:03:32,370
put it down here on line 27. Actually, what
I'll do is I'll come down here and I'll

46
00:03:32,370 --> 00:03:38,800
just, to be consistent with our code,
we're going to take that up one line,

47
00:03:38,800 --> 00:03:45,600
and over here, on line 26 now, I'm going to
type guess, or guesses rather, equals

48
00:03:45,600 --> 00:03:54,600
guesses plus 1. So be careful with the
indentation there. That line must be

49
00:03:54,600 --> 00:03:59,250
indented one level, as I have it there, so
that it's inside the while loop but not

50
00:03:59,250 --> 00:04:04,170
inside the else block. So for now, we're going
to ignore this suggestion from IntelliJ.

51
00:04:04,170 --> 00:04:07,680
I'm going to talk about that in the next
video, but at this point we've now

52
00:04:07,680 --> 00:04:11,459
finished, our program is finished. Check
your code carefully and then in the next

53
00:04:11,459 --> 00:04:17,540
video, we'll test it out to make sure it
works. See you in the next video.

