WEBVTT 1 00:00:00.220 --> 00:00:01.470 Hi and welcome back. 2 00:00:01.470 --> 00:00:04.180 In this video we're going to be constructing our menu. 3 00:00:04.180 --> 00:00:06.203 It's going to be really straightforward. 4 00:00:07.050 --> 00:00:08.960 What we're going to do is we're gonna give the user 5 00:00:08.960 --> 00:00:10.920 some choices to make. 6 00:00:10.920 --> 00:00:12.540 And I'm gonna paste in here, 7 00:00:12.540 --> 00:00:14.383 what exactly the choices gonna be. 8 00:00:16.110 --> 00:00:18.900 So let's say the user has these choices. 9 00:00:18.900 --> 00:00:20.820 They can enter one of the following, 10 00:00:20.820 --> 00:00:23.580 the character B to look at five-star books, 11 00:00:23.580 --> 00:00:27.100 the best books, C to look at the five cheapest books, 12 00:00:27.100 --> 00:00:29.020 or the 10 cheapest books, 13 00:00:29.020 --> 00:00:33.430 N to just get the next available book on the collection, 14 00:00:33.430 --> 00:00:36.760 let's say catalogue here instead 15 00:00:36.760 --> 00:00:38.910 and they can type Q to exit. 16 00:00:38.910 --> 00:00:40.510 And then they gonna enter their choice. 17 00:00:40.510 --> 00:00:43.113 So you know exactly how to do this now. 18 00:00:44.210 --> 00:00:45.853 You have all the tools you need, 19 00:00:47.200 --> 00:00:49.470 this one here has not been implemented for you, 20 00:00:49.470 --> 00:00:51.560 how to get the next available book on the catalogue, 21 00:00:51.560 --> 00:00:54.843 but I'm sure you can use generators to do this. 22 00:00:56.120 --> 00:00:56.990 So give it a go. 23 00:00:56.990 --> 00:00:59.940 Try to implement this menu yourself. 24 00:00:59.940 --> 00:01:04.080 Allow the user to get cheapest books, five-star books, 25 00:01:04.080 --> 00:01:06.690 the next book and also run that on a loop, 26 00:01:06.690 --> 00:01:09.283 so that they can always exit the application. 27 00:01:10.630 --> 00:01:11.670 I hope you managed. 28 00:01:11.670 --> 00:01:14.470 Let's get started on creating our menu. 29 00:01:14.470 --> 00:01:18.253 So down here I'm going to create our menu property here. 30 00:01:19.315 --> 00:01:23.693 And I'm gonna say user input is input of user choice, 31 00:01:24.920 --> 00:01:29.920 while the user input is not Q, we're going to run our menu. 32 00:01:30.120 --> 00:01:34.820 So we're gonna say if user input is equal to B, 33 00:01:34.820 --> 00:01:36.533 we're gonna print the best books. 34 00:01:37.960 --> 00:01:42.530 And if the user input is equal to C, we are gonna print 35 00:01:42.530 --> 00:01:43.813 the cheapest books. 36 00:01:45.380 --> 00:01:48.883 And if the user input is equal to N, 37 00:01:49.790 --> 00:01:51.853 we're going to somehow get the next book. 38 00:01:53.030 --> 00:01:55.090 We're gonna implement that in a moment. 39 00:01:55.090 --> 00:02:00.090 Else we're going to print, please choose a valued command, 40 00:02:00.170 --> 00:02:01.670 I think that's a reasonable thing to do 41 00:02:01.670 --> 00:02:03.763 and finally we're gonna once again, 42 00:02:06.180 --> 00:02:09.240 give them the choice to give us another thing 43 00:02:09.240 --> 00:02:10.480 that they wanna do. 44 00:02:10.480 --> 00:02:11.530 Then at the very end, 45 00:02:13.650 --> 00:02:16.080 we're going to, from the menu. 46 00:02:16.080 --> 00:02:18.893 Let's implement the get next book function. 47 00:02:21.480 --> 00:02:25.230 All that we have to do, in order to get the next book, 48 00:02:25.230 --> 00:02:28.993 is create a generator on the books. 49 00:02:35.590 --> 00:02:37.890 And then get next book 50 00:02:37.890 --> 00:02:42.833 can just print out the next of that. 51 00:02:44.420 --> 00:02:48.570 That is how simple it can be using generators. 52 00:02:48.570 --> 00:02:50.460 You don't have to worry about, you know, 53 00:02:50.460 --> 00:02:53.910 what was the last book you looked at, or keeping an index, 54 00:02:53.910 --> 00:02:57.490 or something, or how many books you've gone through, no. 55 00:02:57.490 --> 00:03:01.440 Just create a generator for the list, very efficient, 56 00:03:01.440 --> 00:03:03.800 because it doesn't actually do anything, 57 00:03:03.800 --> 00:03:06.600 until you start getting the next book. 58 00:03:06.600 --> 00:03:07.890 So it doesn't take up any memory, 59 00:03:07.890 --> 00:03:11.030 or not a lot of memory, it's not completely free, 60 00:03:11.030 --> 00:03:12.690 but it's essentially free. 61 00:03:12.690 --> 00:03:14.640 It doesn't take up any processing power. 62 00:03:14.640 --> 00:03:16.520 Every time you press next, it just gives you 63 00:03:16.520 --> 00:03:18.120 the next book in that. 64 00:03:18.120 --> 00:03:21.220 Generator it doesn't actually go through the entire list, 65 00:03:21.220 --> 00:03:22.320 or anything like that. 66 00:03:23.490 --> 00:03:27.460 So that's a great way of setting up this 67 00:03:27.460 --> 00:03:29.073 getting the next of something. 68 00:03:31.320 --> 00:03:33.020 Let's run it and see what happens. 69 00:03:35.100 --> 00:03:37.050 I get to enter one of the following, 70 00:03:37.050 --> 00:03:42.050 let's type B and then I see sort of the best books in here. 71 00:03:42.690 --> 00:03:45.130 Five-star books, four-star books, it's pretty awesome. 72 00:03:45.130 --> 00:03:46.773 Maybe I wanna buy one of these. 73 00:03:47.620 --> 00:03:49.310 C you can look a the cheapest books, 74 00:03:49.310 --> 00:03:53.440 just in case you're wanting to spend a little less money 75 00:03:53.440 --> 00:03:56.240 on a book, some of them are not so good, 76 00:03:56.240 --> 00:03:57.340 but you know others are pretty good, 77 00:03:57.340 --> 00:04:00.290 so maybe you can find something good there. 78 00:04:00.290 --> 00:04:02.440 And if you just wanna go through the books, 79 00:04:03.550 --> 00:04:06.590 just press N and then you get one, "A Light in the Attic", 80 00:04:06.590 --> 00:04:10.393 N again, "Tipping the Velvet", it's not a very good one, 81 00:04:11.227 --> 00:04:14.260 "Sous Michon", I guess that's French, 82 00:04:14.260 --> 00:04:17.690 or maybe it's some sort of weird submission thing. 83 00:04:17.690 --> 00:04:19.410 That's also not very good. 84 00:04:19.410 --> 00:04:20.380 And that's it. 85 00:04:20.380 --> 00:04:23.500 Now you cannot go back on the catalogue, 86 00:04:23.500 --> 00:04:25.830 I'll leave that to you to implement. 87 00:04:25.830 --> 00:04:27.640 But this is how you can see the different things 88 00:04:27.640 --> 00:04:28.700 that are in the catalogue. 89 00:04:28.700 --> 00:04:30.570 I guess you could go back just by scrolling up. 90 00:04:30.570 --> 00:04:32.797 So that works too. 91 00:04:32.797 --> 00:04:35.023 And you can press Q to terminate. 92 00:04:36.940 --> 00:04:38.880 Okay so there are a few more improvements we're gonna make 93 00:04:38.880 --> 00:04:40.050 on this project. 94 00:04:40.050 --> 00:04:41.280 This first improvement we're gonna make 95 00:04:41.280 --> 00:04:44.900 is we are going to scrape all of the pages in the catalogue 96 00:04:44.900 --> 00:04:46.220 not just one. 97 00:04:46.220 --> 00:04:48.170 That way we're gonna get a much larger collection. 98 00:04:48.170 --> 00:04:50.940 So we're gonna look at how to scrape multiple pages, 99 00:04:50.940 --> 00:04:53.863 how to go through the different pages of this site. 100 00:04:55.060 --> 00:04:58.440 And we can also add logging, just so it's a bit easier 101 00:04:58.440 --> 00:04:59.900 to spot when things go wrong 102 00:04:59.900 --> 00:05:02.120 and also what's goin' on in the programme. 103 00:05:02.120 --> 00:05:04.730 I'd encourage you to try to do these two, 104 00:05:04.730 --> 00:05:06.100 particularly the logging, 105 00:05:06.100 --> 00:05:07.660 because we've already looked at logging, 106 00:05:07.660 --> 00:05:08.630 you know it's quite simple. 107 00:05:08.630 --> 00:05:10.710 All you have to do is import logging, 108 00:05:10.710 --> 00:05:13.430 set up the logger and start logging in different places. 109 00:05:13.430 --> 00:05:15.540 I'd encourage you to do that. 110 00:05:15.540 --> 00:05:18.820 And try to set up the looking at different pages if you can, 111 00:05:18.820 --> 00:05:22.570 but if not just wait and we will do it here. 112 00:05:22.570 --> 00:05:23.410 For the logging, 113 00:05:23.410 --> 00:05:25.980 one of the most important things about logging 114 00:05:25.980 --> 00:05:28.600 and why I'm recommending that you do it 115 00:05:29.930 --> 00:05:34.313 is deciding where to log and what to log. 116 00:05:35.720 --> 00:05:37.770 Because you can log at every line 117 00:05:37.770 --> 00:05:40.580 and that's gonna make your code look really ugly. 118 00:05:40.580 --> 00:05:43.770 Or you can log too little 119 00:05:43.770 --> 00:05:46.300 and then you're not gonna get enough information. 120 00:05:46.300 --> 00:05:49.740 So definitely keep an eye out for that. 121 00:05:49.740 --> 00:05:51.340 Try to implement the logging yourself, 122 00:05:51.340 --> 00:05:54.210 then we'll go back in the video and do it here 123 00:05:54.210 --> 00:05:56.930 just so you can see how much I would log 124 00:05:56.930 --> 00:05:58.030 and where I would log. 125 00:05:59.300 --> 00:06:00.560 That's it for this video. 126 00:06:00.560 --> 00:06:02.160 So I'll see you on the next one.