WEBVTT

1
00:00:01.430 --> 00:00:02.860
<v Jose>Hi Guys, and welcome back.</v>

2
00:00:02.860 --> 00:00:06.080
In this video, we're going to be creating the user menu

3
00:00:06.080 --> 00:00:08.710
for our project so that we can start interacting

4
00:00:08.710 --> 00:00:12.500
with users and looking at what our programme is going to do.

5
00:00:12.500 --> 00:00:15.670
To create the project, I've gone ahead and made a folder.

6
00:00:15.670 --> 00:00:16.970
I've called it Prog diary.

7
00:00:16.970 --> 00:00:18.290
You can call it whatever you want.

8
00:00:18.290 --> 00:00:20.630
And I've opened that with Visual Studio code,

9
00:00:20.630 --> 00:00:24.160
in this case, you can use your editor of choice of course,

10
00:00:24.160 --> 00:00:27.600
in here, I'm going to create a file called app.py.

11
00:00:27.600 --> 00:00:30.320
This is normally the app I use to start a project

12
00:00:30.320 --> 00:00:33.623
it's gonna contain the main logic of our application.

13
00:00:36.590 --> 00:00:38.200
Now that I've got this file here,

14
00:00:38.200 --> 00:00:41.560
we're going to start off by creating the main variable

15
00:00:41.560 --> 00:00:43.060
that is going to be the menu that

16
00:00:43.060 --> 00:00:44.763
we're going to show our users.

17
00:00:46.550 --> 00:00:48.360
So I've gone ahead and written that out,

18
00:00:48.360 --> 00:00:50.480
I've used the multi line string and we've got

19
00:00:50.480 --> 00:00:52.560
the Please select one of the following options,

20
00:00:52.560 --> 00:00:53.600
the three options,

21
00:00:53.600 --> 00:00:55.570
and then their selection with a space at the end

22
00:00:55.570 --> 00:00:58.690
so that users can go ahead and type after this.

23
00:00:58.690 --> 00:01:00.580
We're going to be passing this variable

24
00:01:00.580 --> 00:01:04.163
to the input function so that users can type what they want.

25
00:01:05.070 --> 00:01:06.580
As well as this variable, we're going to need

26
00:01:06.580 --> 00:01:08.920
some sort of welcome variable that we're going to show

27
00:01:08.920 --> 00:01:10.933
at the very start of our application.

28
00:01:14.350 --> 00:01:17.143
So here, I've created those two variables now,

29
00:01:18.620 --> 00:01:21.090
the next step is to have a loop

30
00:01:21.090 --> 00:01:23.840
that is going to run continuously asking the user

31
00:01:23.840 --> 00:01:25.940
what they want to do with our application

32
00:01:25.940 --> 00:01:28.470
until they enter the number three.

33
00:01:28.470 --> 00:01:31.070
So what we normally do when we want to do something

34
00:01:31.070 --> 00:01:33.950
like that is we use a while loop.

35
00:01:33.950 --> 00:01:36.450
So the first thing is to get the user input.

36
00:01:36.450 --> 00:01:39.680
And we're going to make that equal to the input of menu.

37
00:01:39.680 --> 00:01:42.940
So we're going to ask the user showing them this prompt,

38
00:01:42.940 --> 00:01:44.160
what they want to do,

39
00:01:44.160 --> 00:01:48.800
and while the user input is not equal to the number three,

40
00:01:48.800 --> 00:01:50.410
and remember that the input function

41
00:01:50.410 --> 00:01:51.780
always returns a string,

42
00:01:51.780 --> 00:01:55.580
so we need to compare against the number three as a string.

43
00:01:55.580 --> 00:01:57.610
Then we're going to run our menu

44
00:01:57.610 --> 00:01:58.970
we're gonna do here,

45
00:01:58.970 --> 00:02:00.540
whatever that the user wants to do.

46
00:02:00.540 --> 00:02:04.210
So we'll deal with user input here.

47
00:02:04.210 --> 00:02:08.070
Then at the end of the menu, we will ask the user again,

48
00:02:08.070 --> 00:02:10.780
we'll say user input equal input of menu.

49
00:02:10.780 --> 00:02:12.210
In just a moment, we're going to find

50
00:02:12.210 --> 00:02:14.310
a slightly better way of writing this menu

51
00:02:14.310 --> 00:02:16.820
so that we don't have this duplication.

52
00:02:16.820 --> 00:02:19.590
First of all, we ask the user for their choice.

53
00:02:19.590 --> 00:02:21.460
If it's not three, we start the loop,

54
00:02:21.460 --> 00:02:25.330
we do what they want us to do, be that one or two.

55
00:02:25.330 --> 00:02:27.090
And then we ask them again,

56
00:02:27.090 --> 00:02:28.050
when we ask them again,

57
00:02:28.050 --> 00:02:29.610
we go back to the top of the while loop,

58
00:02:29.610 --> 00:02:30.780
and we check again.

59
00:02:30.780 --> 00:02:32.080
And if it's not three, we run this again.

60
00:02:32.080 --> 00:02:34.233
And then we ask them again, and so on.

61
00:02:35.850 --> 00:02:38.530
To reduce duplication, we can use a new

62
00:02:38.530 --> 00:02:40.440
feature of Python 3.8.

63
00:02:40.440 --> 00:02:43.950
If you're using Python 3.8, that is the walrus operator

64
00:02:43.950 --> 00:02:46.290
or the assignment expression.

65
00:02:46.290 --> 00:02:48.070
The assignment expression allows us to put

66
00:02:48.070 --> 00:02:51.363
this input statement right in here,

67
00:02:53.100 --> 00:02:54.340
like that.

68
00:02:54.340 --> 00:02:57.560
And what this says is, while user input

69
00:02:57.560 --> 00:03:00.670
which should be equal to the input of menu

70
00:03:00.670 --> 00:03:05.220
is not equal to three, we will then run this code here.

71
00:03:05.220 --> 00:03:08.160
Now very important that the walrus operator or

72
00:03:08.160 --> 00:03:11.260
the assignment expression has lower precedence

73
00:03:11.260 --> 00:03:12.980
than the comparison.

74
00:03:12.980 --> 00:03:15.940
So when we write it like this, what we're actually

75
00:03:15.940 --> 00:03:19.070
doing is we're saying, while user input,

76
00:03:19.070 --> 00:03:22.430
which is equal to whether input of menu

77
00:03:22.430 --> 00:03:24.590
is not equal to three,

78
00:03:24.590 --> 00:03:26.030
and then we will run this code.

79
00:03:26.030 --> 00:03:27.390
So what this is really doing is,

80
00:03:27.390 --> 00:03:29.580
it's assigning two user input,

81
00:03:29.580 --> 00:03:32.190
whether the input of menu is not equal to three.

82
00:03:32.190 --> 00:03:34.930
So this will end up being true or false.

83
00:03:34.930 --> 00:03:38.620
It's not actually going to contain the content

84
00:03:38.620 --> 00:03:40.310
of what the user entered.

85
00:03:40.310 --> 00:03:43.270
So we have to put some brackets around it

86
00:03:43.270 --> 00:03:45.010
so that this evaluates first.

87
00:03:45.010 --> 00:03:47.350
And now this is what we wanted.

88
00:03:47.350 --> 00:03:52.350
We first give user input the value of calling input of menu.

89
00:03:52.800 --> 00:03:54.950
And then we compare that against three

90
00:03:54.950 --> 00:03:57.060
and we check that it's not equal to it.

91
00:03:57.060 --> 00:03:59.040
Now when we run this code here,

92
00:03:59.040 --> 00:04:02.460
the user input variable contains what the user typed.

93
00:04:02.460 --> 00:04:04.450
So that's what we wanted.

94
00:04:04.450 --> 00:04:06.770
So again, this is a new feature in python 3.8.

95
00:04:06.770 --> 00:04:09.380
If you're not using Python 3.8, you can use

96
00:04:09.380 --> 00:04:11.663
what we just typed a moment ago instead.

97
00:04:12.740 --> 00:04:15.510
So now that we've got our user input, we can go ahead

98
00:04:15.510 --> 00:04:18.950
and write a series of if statements that we can use

99
00:04:18.950 --> 00:04:21.010
to deal with a different options.

100
00:04:21.010 --> 00:04:25.217
So if the user input is equal to one, then we will add,

101
00:04:26.490 --> 00:04:27.800
so we'll say adding,

102
00:04:27.800 --> 00:04:29.510
and for now, I'm just gonna put this print statement.

103
00:04:29.510 --> 00:04:30.450
And here we're gonna add

104
00:04:30.450 --> 00:04:33.060
some more stuff to it in just a moment.

105
00:04:33.060 --> 00:04:36.490
Otherwise, if the user input is equal to two,

106
00:04:36.490 --> 00:04:38.510
then we're gonna print viewing.

107
00:04:38.510 --> 00:04:41.310
And again, later on, we're going to actually be displaying

108
00:04:41.310 --> 00:04:44.693
the the entries that they have in their entries list.

109
00:04:45.550 --> 00:04:49.240
Otherwise, if it's not one or two,

110
00:04:49.240 --> 00:04:50.150
but we know

111
00:04:50.150 --> 00:04:51.750
it's not three, because if it was three,

112
00:04:51.750 --> 00:04:54.160
we wouldn't be running this code to begin with.

113
00:04:54.160 --> 00:04:57.760
So otherwise, we know that it's not one, two or three,

114
00:04:57.760 --> 00:04:59.580
it must be something else that they've typed

115
00:04:59.580 --> 00:05:01.570
and that is an invalid option, so we're gonna

116
00:05:01.570 --> 00:05:04.843
tell them invalid option, please try again.

117
00:05:06.870 --> 00:05:09.570
So these are the three different branches

118
00:05:09.570 --> 00:05:10.870
of our if statement.

119
00:05:10.870 --> 00:05:12.790
And this is going to allow us to start

120
00:05:12.790 --> 00:05:16.650
thinking about our menu a bit better by seeing it in code.

121
00:05:16.650 --> 00:05:18.270
Of course, remember, we still have to use

122
00:05:18.270 --> 00:05:20.760
these welcome variable there to welcome users

123
00:05:20.760 --> 00:05:23.000
to our application, we're gonna print that at the top

124
00:05:23.000 --> 00:05:26.610
outside of the loop because we only want to print that once.

125
00:05:26.610 --> 00:05:29.790
And that's really our complete code for the user menu.

126
00:05:29.790 --> 00:05:31.640
Next up, we're gonna be adding functionality

127
00:05:31.640 --> 00:05:33.530
to keep track of entries.

128
00:05:33.530 --> 00:05:36.620
But we're not going to use a database just yet.

129
00:05:36.620 --> 00:05:40.090
We're going to add SQLite later on in this project.

130
00:05:40.090 --> 00:05:42.330
First, we're going to develop our programme logic

131
00:05:42.330 --> 00:05:46.740
by using a Python list as a simple database table,

132
00:05:46.740 --> 00:05:49.690
and then we're going to use SQLite later on.

133
00:05:49.690 --> 00:05:51.480
Using a list instead of a database

134
00:05:51.480 --> 00:05:53.870
allows us to quickly develop our programme

135
00:05:53.870 --> 00:05:56.450
without worrying about interacting

136
00:05:56.450 --> 00:05:58.400
with the database just yet.

137
00:05:58.400 --> 00:06:00.900
Once we're happy with all our functions, and how they work,

138
00:06:00.900 --> 00:06:03.160
we can read and write from the database

139
00:06:03.160 --> 00:06:04.640
instead of from the list.

140
00:06:04.640 --> 00:06:06.720
And changing that will be straightforward.

141
00:06:06.720 --> 00:06:08.130
And this way, we only have to worry

142
00:06:08.130 --> 00:06:12.090
about one aspect of software development at a time.

143
00:06:12.090 --> 00:06:13.370
All right, in the next video, we're going to

144
00:06:13.370 --> 00:06:16.060
look at what is SQL and then we're going to come back

145
00:06:16.060 --> 00:06:19.500
to this code and use Python lists as a database.

146
00:06:19.500 --> 00:06:21.103
I'll see you in the next one.

