1
00:00:03,969 --> 00:00:08,320
For the next part of the challenge, we
need to display the options again, if the

2
00:00:08,320 --> 00:00:12,610
user enters an invalid choice. The
obvious way to do that is to copy the

3
00:00:12,610 --> 00:00:17,800
print lines, from lines 1 to 7, and paste
them into an else block. So let's have a

4
00:00:17,800 --> 00:00:29,360
go at doing that. I'm gonna add an else block
here, and paste in those choices.

5
00:00:29,360 --> 00:00:36,420
Then I'll make sure they're obviously
indented to the right level, as well. Okay

6
00:00:36,420 --> 00:00:39,670
So that should work. Let's run the
program and make sure that the valid

7
00:00:39,670 --> 00:00:45,429
options still work, and then try an
invalid choice. Alright, so I'm going to

8
00:00:45,429 --> 00:00:51,460
use 3 to go swimming, 4 to have dinner
- that's working. Let's try an invalid

9
00:00:51,460 --> 00:00:55,839
choice, 8 - that's obviously invalid - and we're
now getting the menu displaying again

10
00:00:55,840 --> 00:01:00,840
which is good. I'll enter zero, to make sure
that still works, to quit the program.

11
00:01:00,840 --> 00:01:06,520
Alright so that works, but obviously it's
a little bit untidy. Obviously up here,

12
00:01:06,520 --> 00:01:11,920
in the options on lines 1 to 7 but also
then on lines 17 through 23. It would be a

13
00:01:11,920 --> 00:01:15,940
lot neater to only print the menu in one
place. A function might be the most

14
00:01:15,940 --> 00:01:19,510
appropriate way to do that, but we
haven't covered functions in the course

15
00:01:19,510 --> 00:01:23,650
yet. So this is the bit that requires
some thought, and it's why we made this

16
00:01:23,650 --> 00:01:28,560
part of the challenge,
optional - thinking like a programmer.

17
00:01:28,560 --> 00:01:32,340
One thing you'll find, when you learn to
program, is that the way you think

18
00:01:32,340 --> 00:01:37,220
changes. It happens slowly, over time, as
you work with more and more code.

19
00:01:37,220 --> 00:01:41,580
You'll automatically start looking at things in
different ways, and considering different

20
00:01:41,590 --> 00:01:47,619
options to solve a given problem.
So back to our code. In this example, what

21
00:01:47,619 --> 00:01:52,180
would happen if we requested the input
at the end of the loop, instead of at the

22
00:01:52,180 --> 00:01:56,110
beginning, where it currently is on line
10. Well the first thing that will

23
00:01:56,110 --> 00:01:59,110
happen is, we'll get an error.
So let's actually see that. So I'm going

24
00:01:59,110 --> 00:02:07,229
to take line 10. I'm going to cut
that and move that down, here, to line 23.

25
00:02:07,229 --> 00:02:14,400
Actually, what I'll do is, I'll put a space there, and then
I'll come up here and remove the blank line on line 10.

26
00:02:14,400 --> 00:02:18,680
Now be careful with the indentation when you
move line ten, as I've just done.

27
00:02:18,680 --> 00:02:22,780
It should have the same indentation as it
had before, and be at the same level as

28
00:02:22,780 --> 00:02:27,730
the if, elif and else, which you can see
that it is here. Now I won't run the

29
00:02:27,730 --> 00:02:31,720
program because my IDE's showing a problem
with line 10, and if I hover over the

30
00:02:31,720 --> 00:02:38,230
choice variable there, Name choice can
be not defined. If your IDE doesn't highlight

31
00:02:38,230 --> 00:02:42,640
problems before you run, then run the
program to actually see the error.

32
00:02:42,640 --> 00:02:47,260
Alright, so we need to define choice
before the loop starts. So I'm going to come

33
00:02:47,260 --> 00:02:54,940
down here, basically, above the while, and
start there. So choice equals - now we need

34
00:02:54,940 --> 00:02:58,840
to bind it to some value but what value
should we use here? We know that it

35
00:02:58,840 --> 00:03:03,190
mustn't be 0 because that would cause
the loop to break straight away. We also

36
00:03:03,190 --> 00:03:07,450
don't want to use one of the valid
options 1 through 5. So we can initialize

37
00:03:07,450 --> 00:03:12,670
it to almost anything else, apart from
the strings 1 to 5 and 0. So what I'm

38
00:03:12,670 --> 00:03:20,080
going to do is bind it to the string
value - or the minus sign. So that makes

39
00:03:20,080 --> 00:03:23,340
sure that the variable exists and has a
value but it doesn't have a value that

40
00:03:23,350 --> 00:03:26,859
we're likely to use in our menu. The
error has obviously disappeared now,

41
00:03:26,859 --> 00:03:33,220
on line 11. Let's run the program and see
what happens. So you can see, immediately,

42
00:03:33,220 --> 00:03:37,269
what's happened there, is we're now getting
the menu printing out twice. So now that

43
00:03:37,269 --> 00:03:40,180
we're seeing that, we don't need lines 1 to
7 anymore, so I'm actually going to

44
00:03:40,180 --> 00:03:45,910
remove those, delete them. I'm gonna delete the
blank ones as well. I'm going to rerun this

45
00:03:45,910 --> 00:03:51,579
again, and we've seen this popup before,
The program is already running. We don't want to

46
00:03:51,579 --> 00:03:55,209
be typing into the old version, so I'm
going to choose stop and Rerun, to run the

47
00:03:55,209 --> 00:03:58,819
latest code instead. That's looking
better now, the menu's only printing

48
00:03:58,820 --> 00:04:04,320
once and we're inside the loop, waiting
for input. Let's try some options here,

49
00:04:04,320 --> 00:04:11,900
so option one, Learn Python - You chose 1.
Let's check the other options - 2, 3, 4, 5.

50
00:04:11,900 --> 00:04:16,480
We'll choose an invalid option. It should
hopefully display the menu again, and it

51
00:04:16,488 --> 00:04:24,830
does do that, only 0 to quit. So don't
worry if you struggled with this part of

52
00:04:24,830 --> 00:04:29,030
the challenge. As you read and write more
code, you'll start to consider different

53
00:04:29,030 --> 00:04:33,320
ways of doing the same thing. As long as
you take the time to understand how your

54
00:04:33,320 --> 00:04:37,340
code's working - and you can use the
debugger to help here - you'll start to

55
00:04:37,340 --> 00:04:40,820
look at problems in different ways. Try
different ways to solve a problem and

56
00:04:40,820 --> 00:04:45,560
experiment by changing code, to
understand what effect each line has.

57
00:04:45,560 --> 00:04:48,860
Alright, so the final video coming up in
this section will be very short.

58
00:04:48,860 --> 00:04:53,340
I'm going to use a condition on line 2,
instead of while true.

59
00:04:53,340 --> 00:04:56,320
See you in the next video.

