1
00:00:04,790 --> 00:00:09,559
So this was an easy challenge - probably
easier than some of the previous ones.

2
00:00:09,559 --> 00:00:13,400
We set it because we wanted to emphasize
something that may not be obvious.

3
00:00:13,400 --> 00:00:17,720
If it's not obvious, it won't cause you a
problem in your code, but it may confuse

4
00:00:17,720 --> 00:00:21,320
you when you read someone else's code.
I'm going to start by writing my

5
00:00:21,320 --> 00:00:27,199
solution in the obvious way. We're gonna
down here to line eight. We're going to

6
00:00:27,199 --> 00:00:34,100
add an else, colon. On the next line,
print parentheses double quotes, using a

7
00:00:34,100 --> 00:00:43,039
replacement field with 0 in it, is not a
valid number, closing double quotes dot

8
00:00:43,039 --> 00:00:49,789
format temp. Again, making sure we're keeping
two blank lines between the end of

9
00:00:49,789 --> 00:00:54,109
the function, and the code starting on
line 13. So if the input is a valid

10
00:00:54,109 --> 00:00:58,180
number, our function returns - you can see
that's doing that on line eight.

11
00:00:58,180 --> 00:01:02,740
The obvious way to solve the challenge is to
add an else block, which I've done.

12
00:01:02,740 --> 00:01:07,480
If they enter something that can't be
converted to an int, we print the message.

13
00:01:07,480 --> 00:01:11,960
That code's fine - it works and many
programmers would write it that way.

14
00:01:11,960 --> 00:01:17,240
What may not have been obvious, is that the
else isn't needed. If the return on line

15
00:01:17,240 --> 00:01:21,530
eight is executed, then any code after
that line won't be. The function will

16
00:01:21,530 --> 00:01:26,240
terminate at that point, and return to
the calling code. So that means that some

17
00:01:26,240 --> 00:01:30,650
programmers won't use the else. Instead
what they'll do is something like this, so

18
00:01:30,650 --> 00:01:33,490
I'll comment out the else.

19
00:01:33,630 --> 00:01:39,450
I'm gonna move the indentation back to the
same level as the if. So the print on

20
00:01:39,450 --> 00:01:43,619
line 10 is indented one level back - it's
not part of the if block as such, as it

21
00:01:43,619 --> 00:01:48,240
was before the else. And this will do
exactly the same thing. If a valid number

22
00:01:48,240 --> 00:01:53,320
is entered, the function will return. If the
user enters something that isn't valid,

23
00:01:53,320 --> 00:01:57,620
we get the message printed. To be
clear, we only get to line 10 if

24
00:01:57,630 --> 00:02:01,979
invalid input is entered. So I run the program,
which I'll do now and make sure that

25
00:02:01,979 --> 00:02:06,440
that does work.
I'm gonna enter some random characters.

26
00:02:06,440 --> 00:02:11,460
We get the message - the random characters
I typed is not a valid number. Pressing

27
00:02:11,460 --> 00:02:17,520
enter as I do now, without entering anything, comes back and says it's not a valid number.

28
00:02:17,520 --> 00:02:22,740
When I enter 500, the function
returns the value 500 and we don't get

29
00:02:22,740 --> 00:02:27,069
the message. I'll finish with zero to terminate the program.

30
00:02:27,069 --> 00:02:32,320
So you will find code written that way,
rather than using the else clause.

31
00:02:32,320 --> 00:02:36,720
So what I'm going to do is take a copy of
this code. Well actually, I'll go back to the

32
00:02:36,720 --> 00:02:41,680
project pane, because I want to show you a
comparison of both sets of code.

33
00:02:41,680 --> 00:02:44,360
So we'll copy this.

34
00:02:44,560 --> 00:02:50,000
We'll paste it and I'll call the second
one guessing game else.

35
00:02:50,160 --> 00:02:54,860
With the else code what I'll do, with that version of the file, we're going to leave the else in

36
00:02:54,860 --> 00:03:00,180
and do the indentation. So we've got both
versions now - we've got the else version,

37
00:03:00,180 --> 00:03:04,680
and also the version that we just worked 
on and ran, that hasn't got the else.

38
00:03:04,680 --> 00:03:11,060
Now if we now come up to Window,
select Editor Tabs and split vertically,

39
00:03:15,069 --> 00:03:19,269
I should be able to close these other
windows down now, and we can see both

40
00:03:19,269 --> 00:03:22,109
versions of the code.

41
00:03:23,240 --> 00:03:29,060
You can see there that the comparison,
the left version, is the else version,

42
00:03:29,060 --> 00:03:32,760
and that's got the else clause in, and
the file on the right hand side has got

43
00:03:32,760 --> 00:03:37,020
the else commented out, and the
indentation level of the print is at the

44
00:03:37,020 --> 00:03:42,060
same level as the if statement. So
whichever style you prefer is up to you.

45
00:03:42,060 --> 00:03:45,660
Else is unnecessary, but you may feel
that it makes the code easier to

46
00:03:45,660 --> 00:03:48,390
understand. There's no difference in performance -

47
00:03:48,390 --> 00:03:53,700
they both compile to almost identical
bytecode. Make sure you do understand the

48
00:03:53,700 --> 00:03:58,620
flow of execution here. The return on
line eight will prevent any further code

49
00:03:58,620 --> 00:04:02,520
from executing. If you want to use the
debugger to check, then set a breakpoint

50
00:04:02,520 --> 00:04:05,580
on line seven. You can then experiment with valid and

51
00:04:05,580 --> 00:04:09,340
invalid numbers, to verify that both
these approaches will do the same thing.

52
00:04:09,340 --> 00:04:13,000
When you're happy that both ways of
writing this do exactly the same thing,

53
00:04:13,000 --> 00:04:16,240
I'll see you in the next video.

