WEBVTT 1 00:00:00.340 --> 00:00:01.330 Hi, welcome back. 2 00:00:01.330 --> 00:00:05.170 In this video we're to be developing our milestone project. 3 00:00:05.170 --> 00:00:07.380 If I was going to take this video and a few more videos 4 00:00:07.380 --> 00:00:10.070 since this is a bit of larger application. 5 00:00:11.020 --> 00:00:13.920 What I've added here to our app.py 6 00:00:13.920 --> 00:00:16.360 is a set of bullet points. 7 00:00:16.360 --> 00:00:18.200 And I've put them inside these 8 00:00:18.200 --> 00:00:20.630 triple quotation marks here. 9 00:00:20.630 --> 00:00:23.620 And I know that we've seen this a little bit before, 10 00:00:23.620 --> 00:00:27.760 but just as a reminder, this is a multi-line string. 11 00:00:27.760 --> 00:00:29.940 A string that takes up multiple lines 12 00:00:29.940 --> 00:00:31.900 and it lasts from the three quotation marks 13 00:00:31.900 --> 00:00:34.370 until the three quotation marks at the end. 14 00:00:34.370 --> 00:00:37.720 Basically, Python isn't going to run this 15 00:00:37.720 --> 00:00:38.800 because it's just a string. 16 00:00:38.800 --> 00:00:40.350 It doesn't do anything. 17 00:00:40.350 --> 00:00:42.430 So, it's a good way to type out some stuff 18 00:00:42.430 --> 00:00:43.800 that you wanna know for your programme 19 00:00:43.800 --> 00:00:46.370 or some things that you want to remember. 20 00:00:46.370 --> 00:00:47.420 So I've done that here, 21 00:00:47.420 --> 00:00:49.090 just for a benefit. 22 00:00:49.090 --> 00:00:51.150 I've got our user interface. 23 00:00:51.150 --> 00:00:52.840 You know this from the brief. 24 00:00:53.770 --> 00:00:56.150 Enter a to add a movie, l to see your movies, 25 00:00:56.150 --> 00:00:58.810 f to find a movie and q to quit. 26 00:00:58.810 --> 00:01:01.260 This is the main part of the user interface. 27 00:01:01.260 --> 00:01:02.740 This is what the user is going to see 28 00:01:02.740 --> 00:01:04.380 the first time they run the programme. 29 00:01:04.380 --> 00:01:06.860 And it's what they're going to be able to interact with 30 00:01:06.860 --> 00:01:09.440 by typing a, l, f and q. 31 00:01:10.850 --> 00:01:12.870 Granted this is not a graphical interface. 32 00:01:12.870 --> 00:01:14.050 That's what you'd normally see 33 00:01:14.050 --> 00:01:16.320 when you talk about interfaces. 34 00:01:16.320 --> 00:01:19.730 But it is a text interface and that's totally valid. 35 00:01:19.730 --> 00:01:22.960 So the user is gonna be able to do four things. 36 00:01:22.960 --> 00:01:24.150 Add a new movie. 37 00:01:25.020 --> 00:01:27.690 See the existing movies. 38 00:01:27.690 --> 00:01:29.840 Find a particular movie. 39 00:01:29.840 --> 00:01:32.440 And stop running the programme when they type q. 40 00:01:33.670 --> 00:01:36.560 So let's come up with some tasks that we'll have to do 41 00:01:36.560 --> 00:01:38.770 in order to make our programme functional. 42 00:01:40.020 --> 00:01:44.670 The first task is show the user the main interface 43 00:01:44.670 --> 00:01:45.840 and get their input. 44 00:01:48.110 --> 00:01:50.180 The second task is going to be 45 00:01:51.200 --> 00:01:55.350 to allow users to add movies. 46 00:01:56.860 --> 00:02:01.070 Then we're gonna allows users to show all their movies. 47 00:02:02.740 --> 00:02:05.660 Find a movie. 48 00:02:06.830 --> 00:02:09.350 Finally, we will allow users to also 49 00:02:09.350 --> 00:02:14.220 stop running the programme when they type q. 50 00:02:15.250 --> 00:02:17.780 As we start developing the programme, 51 00:02:17.780 --> 00:02:21.040 we will be adding more tasks to this list of tasks 52 00:02:21.040 --> 00:02:23.750 so that, because this is going to be a sort of 53 00:02:23.750 --> 00:02:27.310 live set of tasks as we go along. 54 00:02:27.310 --> 00:02:29.940 For example, as we start developing, 55 00:02:29.940 --> 00:02:33.920 we may realise, "Hey, what is a movie? 56 00:02:33.920 --> 00:02:37.550 "Where are we going to store our movies?" 57 00:02:37.550 --> 00:02:39.120 In a larger app we may store them 58 00:02:39.120 --> 00:02:41.030 on a database, for example. 59 00:02:41.030 --> 00:02:46.030 But here, we're gonna have to decide where to store movies. 60 00:02:48.080 --> 00:02:50.640 What is the format of a movie? 61 00:02:51.680 --> 00:02:54.630 So these are a couple of tasks that we'll have to think of. 62 00:02:55.820 --> 00:02:56.653 Okay? 63 00:02:58.050 --> 00:03:01.890 Let's start with defining the user interface. 64 00:03:01.890 --> 00:03:04.000 This is our menu. 65 00:03:04.000 --> 00:03:05.130 And the first thing we wanna do is 66 00:03:05.130 --> 00:03:07.750 ask the user this first question. 67 00:03:07.750 --> 00:03:10.270 What they can type in order to interact with our programme. 68 00:03:10.270 --> 00:03:12.300 So I'm going to save it into a 69 00:03:12.300 --> 00:03:15.010 user underscore input variable, 70 00:03:15.010 --> 00:03:17.930 the result of this input function here. 71 00:03:17.930 --> 00:03:21.130 And we're going to, oh, sorry, 72 00:03:21.130 --> 00:03:24.380 copy all that and put it in there. 73 00:03:24.380 --> 00:03:27.410 Now this is a bit of a longer string. 74 00:03:27.410 --> 00:03:29.940 My font is quite big for you guys to be able to see it, 75 00:03:29.940 --> 00:03:31.170 but it just says that. 76 00:03:31.170 --> 00:03:32.690 And I'm gonna add a colon at the end 77 00:03:32.690 --> 00:03:34.640 then a space, just so it's a bit nicer. 78 00:03:35.870 --> 00:03:36.720 There we have it. 79 00:03:38.010 --> 00:03:41.840 Now, as you develop, you may notice these grey lines. 80 00:03:42.800 --> 00:03:46.840 And PyCharm just tells you that, "Hey, Python styling 81 00:03:46.840 --> 00:03:50.510 "has some particularities that you may want to follow, 82 00:03:50.510 --> 00:03:52.900 "and one of them is that it expects two blank lines 83 00:03:52.900 --> 00:03:55.470 "before a function definition as opposed to just one." 84 00:03:55.470 --> 00:03:59.120 So if you add a new blank line on top, it'll be happy. 85 00:03:59.120 --> 00:04:01.410 Similarly here it says, "You've got a blank line 86 00:04:01.410 --> 00:04:04.930 "at the end of the file and, well, 87 00:04:04.930 --> 00:04:06.090 "you've got too many of them." 88 00:04:06.090 --> 00:04:08.280 So, if you only have one, then it'll be happy, 89 00:04:08.280 --> 00:04:10.570 but, of course, we're gonna continue developing here. 90 00:04:10.570 --> 00:04:13.210 So, as you see these squiggly grey lines, 91 00:04:13.210 --> 00:04:16.470 feel free to search in Google to see what they mean. 92 00:04:16.470 --> 00:04:19.030 And then you can maybe get rid of them. 93 00:04:19.030 --> 00:04:22.120 Although sometimes PEP 8 can be a bit ... 94 00:04:22.120 --> 00:04:27.120 By the way, PEP 8 is the Python styling thing. 95 00:04:27.950 --> 00:04:31.070 It can be a bit cumbersome because it constrains you a bit. 96 00:04:31.070 --> 00:04:32.700 It can constrain you a bit, so 97 00:04:32.700 --> 00:04:35.470 just remember you can ignore it if you want. 98 00:04:35.470 --> 00:04:37.450 Anyway, small side thing. 99 00:04:37.450 --> 00:04:39.800 We've asked the user for their input 100 00:04:39.800 --> 00:04:42.320 and now what we want to do is deal with it. 101 00:04:42.320 --> 00:04:47.320 So, if user_input is a, we're gonna do something. 102 00:04:47.360 --> 00:04:51.300 And what we're gonna do is we're going to add a new movie. 103 00:04:51.300 --> 00:04:54.560 But we don't have the functionality to add a movie yet. 104 00:04:54.560 --> 00:04:56.810 But we know that we're gonna add it later on. 105 00:04:57.830 --> 00:04:58.930 And now there are a couple of questions 106 00:04:58.930 --> 00:05:00.110 we have to ask ourselves. 107 00:05:00.110 --> 00:05:04.490 The first one is, does the add_movie functionality 108 00:05:04.490 --> 00:05:06.930 depend on anything inside this function? 109 00:05:08.246 --> 00:05:09.079 And the answer is no, 110 00:05:09.079 --> 00:05:10.450 because, once we've reached this point, 111 00:05:10.450 --> 00:05:13.450 user_input's already fulfilled it's purpose. 112 00:05:14.700 --> 00:05:18.940 So the add_movie part here of our programme 113 00:05:18.940 --> 00:05:20.930 is going to deal with asking the user 114 00:05:20.930 --> 00:05:24.900 for what they want to add, for example, the movie name, 115 00:05:26.270 --> 00:05:28.210 and is going to deal with, actually, 116 00:05:28.210 --> 00:05:30.170 adding the movie to whatever it is 117 00:05:30.170 --> 00:05:32.470 that we're going to store our movie. 118 00:05:32.470 --> 00:05:36.080 So we can just type, add underscore movie, 119 00:05:36.080 --> 00:05:38.640 calling this function that doesn't exist yet. 120 00:05:38.640 --> 00:05:40.450 But we're going to add it later on. 121 00:05:41.300 --> 00:05:43.720 Notice how now you get a red squiggly line that says 122 00:05:43.720 --> 00:05:45.200 unresolved reference because 123 00:05:45.200 --> 00:05:48.600 this thing here that we're using doesn't exist. 124 00:05:48.600 --> 00:05:50.320 This is fine. 125 00:05:50.320 --> 00:05:54.720 We now know that we need to add a function called add_movie 126 00:05:54.720 --> 00:05:57.710 that is going to deal with adding the movie. 127 00:05:57.710 --> 00:05:59.420 And that's a pretty nice thing to know 128 00:05:59.420 --> 00:06:03.010 because our programme is now broken down into two parts. 129 00:06:03.010 --> 00:06:05.180 The menu and the add_movie function. 130 00:06:05.180 --> 00:06:09.410 And we're gonna do the same thing for the other functions. 131 00:06:09.410 --> 00:06:10.810 The other pieces of functionality. 132 00:06:10.810 --> 00:06:15.750 So, if user_input is equal to l, we're going to show movies. 133 00:06:17.070 --> 00:06:20.580 If user_input is equal to f, we're going to find movie. 134 00:06:21.980 --> 00:06:26.980 And, otherwise, we're going to just not do anything. 135 00:06:27.960 --> 00:06:30.740 If user_input is equal to q, 136 00:06:30.740 --> 00:06:33.410 we can print something like, "stopping programme". 137 00:06:34.960 --> 00:06:35.793 Okay? 138 00:06:37.530 --> 00:06:40.420 Now, something you may notice when you do this, 139 00:06:40.420 --> 00:06:43.730 is that we've got an if statement here, 140 00:06:43.730 --> 00:06:44.870 followed by another if statement, 141 00:06:44.870 --> 00:06:45.790 followed by another if statement, 142 00:06:45.790 --> 00:06:46.790 followed by another if statement. 143 00:06:46.790 --> 00:06:49.940 So, we've got four independent ifs. 144 00:06:49.940 --> 00:06:52.820 Remember, an if statement is always independent 145 00:06:52.820 --> 00:06:54.500 from any other if statements, 146 00:06:54.500 --> 00:06:58.120 unless you do something like elif or else. 147 00:07:02.010 --> 00:07:04.360 And all these if statements are going to run 148 00:07:04.360 --> 00:07:05.820 one after another. 149 00:07:05.820 --> 00:07:08.140 So, when the user types, say, a 150 00:07:09.100 --> 00:07:09.933 the first thing we're gonna do 151 00:07:09.933 --> 00:07:12.790 is check whether the user_input is a, which it will be. 152 00:07:12.790 --> 00:07:15.770 So we will run this function, once we've made it. 153 00:07:15.770 --> 00:07:18.400 Then we will check whether the user_input is l, 154 00:07:18.400 --> 00:07:20.630 which it cannot be because it was a. 155 00:07:20.630 --> 00:07:22.190 So we will not run this. 156 00:07:22.190 --> 00:07:24.480 Then, we will check whether the user_input was f, 157 00:07:24.480 --> 00:07:26.700 which it cannot be because it was a. 158 00:07:26.700 --> 00:07:28.300 And that we will not run this. 159 00:07:28.300 --> 00:07:30.050 Then we will check whether the user_input was q, 160 00:07:30.050 --> 00:07:32.300 which it cannot be because it was a, 161 00:07:32.300 --> 00:07:34.850 and then we won't print that. 162 00:07:34.850 --> 00:07:36.140 So, as you can see, what I'm saying here 163 00:07:36.140 --> 00:07:38.380 is that all these if statements are related. 164 00:07:38.380 --> 00:07:42.430 If one of these runs, the others cannot possibly run, 165 00:07:42.430 --> 00:07:46.240 because user_input cannot both be a and l. 166 00:07:46.240 --> 00:07:48.580 What to do is turn them into elifs. 167 00:07:54.350 --> 00:07:56.650 Now, when the user types a, 168 00:07:56.650 --> 00:07:58.200 we're going to add a new movie. 169 00:07:59.810 --> 00:08:04.260 And then we want to repeat this entire thing 170 00:08:04.260 --> 00:08:07.600 and allow the user to type something else, 171 00:08:07.600 --> 00:08:09.250 like l, for example. 172 00:08:09.250 --> 00:08:10.950 Then we wanna run the entire thing again 173 00:08:10.950 --> 00:08:13.060 and ask the user to type something else. 174 00:08:13.060 --> 00:08:15.080 And at that point, they may type q 175 00:08:15.080 --> 00:08:16.680 and then we'll stop the programme. 176 00:08:16.680 --> 00:08:18.860 We won't ask them anything else. 177 00:08:18.860 --> 00:08:21.490 So, what I'm saying here is that there needs to be a loop 178 00:08:21.490 --> 00:08:24.150 that will repeat this over and over and over 179 00:08:24.150 --> 00:08:26.040 until the user types q. 180 00:08:27.420 --> 00:08:29.590 So, how are we gonna do that? 181 00:08:29.590 --> 00:08:31.770 Well, I think that the best thing to do 182 00:08:31.770 --> 00:08:36.170 would be to add a while loop that says that 183 00:08:36.170 --> 00:08:39.730 while the user_input is not q, we'll run this 184 00:08:39.730 --> 00:08:41.750 and as soon as the user_input is q, 185 00:08:41.750 --> 00:08:42.870 we will stop running. 186 00:08:43.990 --> 00:08:46.530 Of course these things have to be more indented, 187 00:08:47.450 --> 00:08:52.450 so that we put all of this inside this loop here. 188 00:08:56.030 --> 00:08:57.910 But notice, it's something that 189 00:08:57.910 --> 00:09:00.420 you probably won't notice until you actually run the app 190 00:09:00.420 --> 00:09:02.320 unless you've done this a few times, 191 00:09:02.320 --> 00:09:07.020 that the user_input is asked at the start of the function, 192 00:09:07.020 --> 00:09:08.960 but not inside the loop. 193 00:09:09.940 --> 00:09:12.960 So we'll ask the user for their input 194 00:09:12.960 --> 00:09:16.400 and then we will just continue running this endlessly 195 00:09:16.400 --> 00:09:18.890 without ever asking the user for input again 196 00:09:18.890 --> 00:09:22.150 because this input line is outside the loop. 197 00:09:22.150 --> 00:09:24.620 So what we want to do is make sure that this line 198 00:09:24.620 --> 00:09:26.350 is also in the loop. 199 00:09:26.350 --> 00:09:30.050 And what we're gonna do is just, get it all, copy it 200 00:09:30.050 --> 00:09:31.980 and put it at the end of the loop. 201 00:09:32.930 --> 00:09:35.140 The reason I'm putting it at the end and not at the start 202 00:09:35.140 --> 00:09:37.550 is that, if I put it at the start, then we'll ask 203 00:09:37.550 --> 00:09:38.810 the user for input 204 00:09:38.810 --> 00:09:40.160 and then as soon as we go into the loop, 205 00:09:40.160 --> 00:09:41.810 we'll ask the user for input again, 206 00:09:41.810 --> 00:09:45.670 which will be mildly weird for the user. 207 00:09:47.120 --> 00:09:48.250 Okay? 208 00:09:48.250 --> 00:09:50.140 And this makes us realise that 209 00:09:51.340 --> 00:09:53.740 if the user types q to begin with, 210 00:09:54.630 --> 00:09:59.630 this loop is not going to run because q will be equal to q. 211 00:09:59.630 --> 00:10:00.880 So nothing here will run. 212 00:10:02.150 --> 00:10:07.150 If the user types a, we will go in here and add a new movie. 213 00:10:07.200 --> 00:10:10.040 Then we will ask them again, and let's say they type q. 214 00:10:11.180 --> 00:10:12.450 Then the loop will stop running 215 00:10:12.450 --> 00:10:13.570 because we'll go back to the top, 216 00:10:13.570 --> 00:10:17.390 we'll check this and it will be false, 217 00:10:17.390 --> 00:10:19.890 so it will stop and it will continue at the end. 218 00:10:20.900 --> 00:10:23.300 So we're never going to go into this if statement. 219 00:10:23.300 --> 00:10:24.770 That's what I'm saying. 220 00:10:24.770 --> 00:10:28.010 Because if the user_input is q at any point, 221 00:10:28.010 --> 00:10:29.670 this loop is not going to run. 222 00:10:30.580 --> 00:10:32.080 So, we can just get rid of it. 223 00:10:33.570 --> 00:10:35.700 However, something that we may want to use, 224 00:10:35.700 --> 00:10:39.840 is an else here, that says print something like 225 00:10:39.840 --> 00:10:44.840 unknown command-please try again. 226 00:10:46.320 --> 00:10:48.750 We can print that and then the user knows that, 227 00:10:48.750 --> 00:10:50.100 "Hey, we're not messing around. 228 00:10:50.100 --> 00:10:52.520 "You can only enter what we've told you you can enter." 229 00:10:53.500 --> 00:10:54.333 Okay? 230 00:10:55.540 --> 00:10:59.390 Now this is our menu and we can run our menu function 231 00:10:59.390 --> 00:11:00.490 if we choose to. 232 00:11:01.340 --> 00:11:05.730 And notice how, once again, we get this PEP 8, 233 00:11:05.730 --> 00:11:08.000 and it says, "expected, two blank lines ...," 234 00:11:08.000 --> 00:11:10.230 so we have to put two blank lines in there. 235 00:11:10.230 --> 00:11:13.040 And then we still get a small squiggly line and it says, 236 00:11:13.040 --> 00:11:14.790 "No new line at the end of the file." 237 00:11:14.790 --> 00:11:17.580 So, it wants an empty line at the end of the file. 238 00:11:17.580 --> 00:11:20.330 And once you put it in, all the squiggly lines disappear. 239 00:11:20.330 --> 00:11:23.080 Now we only have three squiggly lines that are red 240 00:11:23.080 --> 00:11:26.100 that tells us our functions haven't been defined yet. 241 00:11:26.100 --> 00:11:29.860 Let's run our app, first of all, to check how we're doing, 242 00:11:29.860 --> 00:11:31.530 and then, we can continue in the next video 243 00:11:31.530 --> 00:11:33.090 with the other stuff. 244 00:11:33.090 --> 00:11:35.910 Make sure you call your menu function. 245 00:11:35.910 --> 00:11:39.070 Then go and right-click on app and press Run. 246 00:11:40.810 --> 00:11:41.960 Then you'll get this here, 247 00:11:41.960 --> 00:11:42.830 and I know it's a bit small. 248 00:11:42.830 --> 00:11:45.534 I'm gonna zoom in when I'm editing the video. 249 00:11:45.534 --> 00:11:47.900 And you've got, "Enter 'a' to add a movie, 250 00:11:47.900 --> 00:11:49.540 "'l' to see your movies, 'f' to find a movie, 251 00:11:49.540 --> 00:11:51.300 "and 'q' to quit." 252 00:11:51.300 --> 00:11:54.010 So, we can type something like a. 253 00:11:54.010 --> 00:11:56.460 What do you think's gonna happen when we do that? 254 00:12:00.060 --> 00:12:00.893 I'm sure you got it. 255 00:12:00.893 --> 00:12:02.680 We're probably gonna get some sort of error, right? 256 00:12:02.680 --> 00:12:05.010 'cause we've not defined add_movie. 257 00:12:06.100 --> 00:12:06.933 And that's correct. 258 00:12:06.933 --> 00:12:08.440 We get a NameError that says, 259 00:12:08.440 --> 00:12:10.130 "name 'add_movie' is not defined." 260 00:12:10.130 --> 00:12:12.940 So you've used this thing here, but it doesn't exist. 261 00:12:12.940 --> 00:12:13.930 So, okay, that's fine. 262 00:12:13.930 --> 00:12:15.460 That's as expected. 263 00:12:15.460 --> 00:12:17.790 Notice how our programme completely stopped. 264 00:12:17.790 --> 00:12:19.260 We exited the while loop. 265 00:12:19.260 --> 00:12:21.060 Everything went haywire. 266 00:12:21.060 --> 00:12:23.130 Our programme crashed, essentially. 267 00:12:23.130 --> 00:12:23.963 Not great. 268 00:12:23.963 --> 00:12:25.840 We'll have to define that function later on. 269 00:12:25.840 --> 00:12:28.420 Let's run it again and then type q. 270 00:12:30.450 --> 00:12:32.990 Then the programme just finishes, okay? 271 00:12:34.460 --> 00:12:36.890 If you want, feel free to, after the while loop, 272 00:12:36.890 --> 00:12:39.140 add an if statement, or before the while loop 273 00:12:40.400 --> 00:12:44.590 that says, if you've typed q, exit the programme or something. 274 00:12:44.590 --> 00:12:47.390 Feel free to play around with this code. 275 00:12:47.390 --> 00:12:48.790 This is gonna be your application. 276 00:12:48.790 --> 00:12:51.230 So, feel free to add any functionality you want. 277 00:12:51.230 --> 00:12:53.960 And, of course, if you've not yet attempted 278 00:12:53.960 --> 00:12:55.910 to develop this programme on your own 279 00:12:55.910 --> 00:12:59.140 and you feel like now you are more capable of doing so, 280 00:12:59.140 --> 00:13:02.470 please, before moving on to the next video, 281 00:13:02.470 --> 00:13:05.350 go ahead and try typing all of this out 282 00:13:05.350 --> 00:13:07.980 and continue developing these three functions. 283 00:13:07.980 --> 00:13:09.640 Again, by doing this yourself, 284 00:13:09.640 --> 00:13:11.500 is the best way to learn. 285 00:13:11.500 --> 00:13:13.840 So if you can develop it yourself, do so. 286 00:13:13.840 --> 00:13:16.160 And then you can move onto the next video 287 00:13:16.160 --> 00:13:18.210 and check how we've done it here. 288 00:13:19.330 --> 00:13:20.670 So that's it for this video. 289 00:13:20.670 --> 00:13:22.080 I'll see you on the next one.