1 00:00:00,290 --> 00:00:04,650 With our database ready to go, we need to start building out our application. 2 00:00:04,650 --> 00:00:06,640 Let's start with the menu function. 3 00:00:06,640 --> 00:00:10,693 It'll print out the menu options to the console, ask the user for 4 00:00:10,693 --> 00:00:15,496 their choice, and then return their choice for use in our main app function. 5 00:00:20,263 --> 00:00:21,820 Couple spaces. 6 00:00:23,630 --> 00:00:30,660 def menu and then inside we're going to create a while loop. 7 00:00:31,980 --> 00:00:34,810 Do while True. 8 00:00:34,810 --> 00:00:37,360 And then let's print out our menu options. 9 00:00:37,360 --> 00:00:40,675 Print, two, three, four, five, six, and 10 00:00:40,675 --> 00:00:44,760 I'm doing the triple so that we can do multi-line here. 11 00:00:46,300 --> 00:00:52,679 And let's do, I'm gonna do caps lock, PROGRAMMING BOOKS. 12 00:00:55,815 --> 00:00:57,960 1 is going to be Add book. 13 00:00:59,210 --> 00:01:02,620 2 is going to be View all books. 14 00:01:03,830 --> 00:01:08,823 3, Search for book. 15 00:01:08,823 --> 00:01:13,359 4, Book analysis, 16 00:01:13,359 --> 00:01:16,639 and 5, Exit. 17 00:01:16,639 --> 00:01:20,430 Great, and let's do the input, too. 18 00:01:20,430 --> 00:01:26,931 So let's do input and let's just say, 19 00:01:26,931 --> 00:01:31,269 What would you like to do? 20 00:01:31,269 --> 00:01:35,380 And that space, okay, don't worry, we'll capture it in a second. 21 00:01:35,380 --> 00:01:38,920 But let's come down here to the bottom. 22 00:01:38,920 --> 00:01:41,300 And let's call our menu function. 23 00:01:42,540 --> 00:01:46,663 And let's give it a look in the console. 24 00:01:46,663 --> 00:01:49,853 python3 app.py, cool, and 25 00:01:49,853 --> 00:01:55,507 you can see our input is starting over here on the left. 26 00:01:55,507 --> 00:02:00,435 But all of this is getting our indent that's coming from the indent 27 00:02:00,435 --> 00:02:02,996 here to be inside of our function. 28 00:02:02,996 --> 00:02:06,934 And inside of our while loop and inside of our print function call. 29 00:02:06,934 --> 00:02:13,771 So to counteract that, I'm going to say, let's start you on a new line with a \n. 30 00:02:13,771 --> 00:02:16,520 And then for the rest of these, we'll do returns. 31 00:02:16,520 --> 00:02:19,320 It's like hitting the Return or Enter key on your keyboard. 32 00:02:19,320 --> 00:02:23,291 So \ return, \return, \return, 33 00:02:23,291 --> 00:02:27,680 \return, \return, okay, save. 34 00:02:27,680 --> 00:02:34,670 And then to cancel this, we're gonna hit Ctrl+C, and that'll cancel your program. 35 00:02:34,670 --> 00:02:36,673 I'm just gonna run clear. 36 00:02:36,673 --> 00:02:38,912 And then python3 app.py again. 37 00:02:41,485 --> 00:02:43,220 Cool, okay, that looks much better. 38 00:02:43,220 --> 00:02:45,050 It's over here on the left and it looks nice and neat. 39 00:02:46,100 --> 00:02:50,120 I'm gonna go ahead and hit Ctrl+C and clear it again. 40 00:02:50,120 --> 00:02:54,527 Okay, awesome, now we need to ask for the user's input and 41 00:02:54,527 --> 00:02:56,920 we need to save it to a variable. 42 00:02:56,920 --> 00:03:00,820 So we have the ask, let's save it to a variable called choice. 43 00:03:01,990 --> 00:03:06,777 And we also need to make sure that the choice they give us is actually one of 44 00:03:06,777 --> 00:03:09,020 the available options. 45 00:03:09,020 --> 00:03:13,203 So let's do, if choice in, if it's in, and 46 00:03:13,203 --> 00:03:19,140 let's just do a list of our options because it's quite short. 47 00:03:19,140 --> 00:03:23,604 So 1, 2, 3, 4, 5, so 48 00:03:23,604 --> 00:03:28,448 if it's one of these options, 49 00:03:28,448 --> 00:03:33,310 then we can return the choice. 50 00:03:35,070 --> 00:03:38,923 And if it's not, it'll just cause the loop to go back up top and 51 00:03:38,923 --> 00:03:41,880 print out our programming books again. 52 00:03:41,880 --> 00:03:44,430 And it will try again. 53 00:03:45,530 --> 00:03:50,540 But to make it a little bit easier on the user, let's also add an else. 54 00:03:52,260 --> 00:03:54,410 And then let's ask them to try again. 55 00:03:54,410 --> 00:03:57,220 That way they know that they've made a mistake. 56 00:03:57,220 --> 00:04:02,354 So I'm gonna do, Please choose 57 00:04:02,354 --> 00:04:06,544 one of the options above. 58 00:04:06,544 --> 00:04:11,164 And I'm actually gonna make this triple quotes here, one, two, 59 00:04:11,164 --> 00:04:14,230 three, let's go to the end, two, three. 60 00:04:15,240 --> 00:04:17,630 Okay, then I can do this. 61 00:04:17,630 --> 00:04:22,010 Please choose one of the options above. 62 00:04:22,010 --> 00:04:26,248 A number from 1-5. 63 00:04:28,381 --> 00:04:31,350 Press enter to try again. 64 00:04:31,350 --> 00:04:35,421 Okay, this way they receive some sort of message before it kicks them back up to 65 00:04:35,421 --> 00:04:37,190 the start of the loop. 66 00:04:37,190 --> 00:04:40,550 Otherwise, they might think they entered something correctly, 67 00:04:40,550 --> 00:04:41,993 had a little keyboard flub. 68 00:04:41,993 --> 00:04:45,078 And they thought they pushed 1 and really pushed the key next to 1, 69 00:04:45,078 --> 00:04:46,333 that little tilde symbol. 70 00:04:46,333 --> 00:04:50,050 And instead of just seeing the menu again and being like, 71 00:04:50,050 --> 00:04:52,070 wow, your program is broken. 72 00:04:52,070 --> 00:04:54,373 They'll get a nice little error message and realize, oops, 73 00:04:54,373 --> 00:04:57,700 they must have typed something incorrectly and they can try again. 74 00:04:57,700 --> 00:04:59,470 Okay, so let's test this out in the console. 75 00:05:01,430 --> 00:05:02,450 So what would you like to do? 76 00:05:02,450 --> 00:05:03,600 Let's try just a letter, t. 77 00:05:04,720 --> 00:05:06,192 Please choose one of the options above. 78 00:05:06,192 --> 00:05:09,700 And you can see it's getting this spacing again. 79 00:05:09,700 --> 00:05:13,710 So I'm gonna do, I'm gonna try doing \r. 80 00:05:16,317 --> 00:05:18,960 Save that, okay, but we can still test. 81 00:05:18,960 --> 00:05:23,616 Let's just try doing 14, a number that's not in our range, and 82 00:05:23,616 --> 00:05:26,610 we still get the error, perfect. 83 00:05:26,610 --> 00:05:29,684 If we do one of the correct numbers, it should return a choice, 84 00:05:29,684 --> 00:05:31,140 which we're not capturing. 85 00:05:31,140 --> 00:05:35,190 So it should just end our function, perfect. 86 00:05:35,190 --> 00:05:36,240 That's the way it's supposed to work. 87 00:05:36,240 --> 00:05:39,960 And I'm gonna run it one more time just to make sure that error is aligned 88 00:05:39,960 --> 00:05:40,740 a bit nicer. 89 00:05:40,740 --> 00:05:43,720 So I'm just do the letter t, there we go. 90 00:05:43,720 --> 00:05:45,306 Okay, that's nicely aligned over there. 91 00:05:45,306 --> 00:05:50,980 Cool, keyboard interrupt, clear. 92 00:05:50,980 --> 00:05:54,080 And our menu function's good to go. 93 00:05:54,080 --> 00:05:58,433 And it's important to note, the reason why I'm using while True here is because 94 00:05:58,433 --> 00:06:02,570 no matter what, this function will only end when this choice is returned. 95 00:06:02,570 --> 00:06:07,050 And return will always cancel out or stop a loop. 96 00:06:07,050 --> 00:06:08,740 So that's why I used while True. 97 00:06:10,100 --> 00:06:12,240 Next, let's create our app function. 98 00:06:13,480 --> 00:06:17,140 Scroll down here to the bottom because it's probably going to use a lot of 99 00:06:17,140 --> 00:06:19,380 the functions we're gonna be creating. 100 00:06:19,380 --> 00:06:24,267 And so this function will need to go at the end right here before the dunder main. 101 00:06:24,267 --> 00:06:28,112 Add a couple spaces, and def app. 102 00:06:29,833 --> 00:06:33,257 Inside of our function, we'll need a while loop to control our app, 103 00:06:33,257 --> 00:06:36,310 kind of like what we did with the menu up above. 104 00:06:36,310 --> 00:06:40,092 So that when the file is run, the loop will continue to let the user interact 105 00:06:40,092 --> 00:06:42,810 with the database until they decide they wanna exit. 106 00:06:43,810 --> 00:06:48,260 Let's create a variable called app_running and set it equal to True. 107 00:06:49,580 --> 00:06:57,830 And then let's do while app_running, perfect. 108 00:06:57,830 --> 00:07:00,936 The first thing we want our users to see is that main menu so 109 00:07:00,936 --> 00:07:03,200 that they know what options they have. 110 00:07:03,200 --> 00:07:05,260 So we can call the menu. 111 00:07:05,260 --> 00:07:11,750 And remember the menu, let me scroll up, is going to return their choice. 112 00:07:11,750 --> 00:07:16,485 So we wanna capture that by setting menu equal to a variable. 113 00:07:16,485 --> 00:07:20,360 And we can just call it choice so that it's still their menu choice. 114 00:07:22,570 --> 00:07:26,420 Now that we have it, we can use if statements to control what happens next. 115 00:07:26,420 --> 00:07:32,857 So if choice is equal to 1 then that means from our menu above. 116 00:07:32,857 --> 00:07:38,418 And I'll scroll again, so you all can see, we have 1 is Add book. 117 00:07:40,676 --> 00:07:44,970 So I'm gonna do 1 and I'm gonna do a little note here, this is add book. 118 00:07:44,970 --> 00:07:50,026 And I'm just gonna put pass inside for 119 00:07:50,026 --> 00:07:56,193 right now cuz we'll add all this stuff later. 120 00:07:56,193 --> 00:08:03,188 And then elif choice equals 2, this was view books, pass. 121 00:08:03,188 --> 00:08:07,927 Elif choice equals 3 and 122 00:08:07,927 --> 00:08:14,410 then this was search for book. 123 00:08:14,410 --> 00:08:15,580 You need to put in pass. 124 00:08:16,700 --> 00:08:22,913 And then we need elif choice equals 4 and 125 00:08:22,913 --> 00:08:29,140 this is the book analysis, pass. 126 00:08:29,140 --> 00:08:33,206 And here we can just use else because our menu function is already taking 127 00:08:33,206 --> 00:08:36,210 care of any other numbers besides one through five. 128 00:08:36,210 --> 00:08:39,653 So we know we're only gonna get numbers one through five or, 129 00:08:39,653 --> 00:08:42,680 I guess, string numbers one through five. 130 00:08:42,680 --> 00:08:45,979 So we can just use else and 131 00:08:45,979 --> 00:08:51,810 then we can put GOODBYE, Print, GOODBYE. 132 00:08:51,810 --> 00:08:58,090 And then we can set app_running equal to False to turn off our loop. 133 00:08:59,110 --> 00:09:02,530 Now inside of dunder main, let me save. 134 00:09:02,530 --> 00:09:04,320 We don't need to call menu anymore. 135 00:09:04,320 --> 00:09:05,830 Instead, we can just call our app. 136 00:09:06,910 --> 00:09:07,921 Let's run the file. 137 00:09:10,330 --> 00:09:13,600 And awesome, our menu's getting printed out still. 138 00:09:13,600 --> 00:09:17,370 And now we can test out all of our menu options. 139 00:09:17,370 --> 00:09:20,540 So if I type in a letter, I should still get an error. 140 00:09:21,660 --> 00:09:24,060 Awesome, I get the menu again. 141 00:09:24,060 --> 00:09:30,610 If I type in 12, that's not an option, still get the error, perfect. 142 00:09:30,610 --> 00:09:36,285 If I type in 1, because we have nothing in any of these choices, except for 143 00:09:36,285 --> 00:09:42,410 our Exit choice, it should just cause the while loop to start back up the top. 144 00:09:42,410 --> 00:09:46,382 So if I do 1 I still get the menu, 2, 3, 4, and 145 00:09:46,382 --> 00:09:49,900 now when I hit 5, it should close this out. 146 00:09:51,520 --> 00:09:54,600 And there we go, perfect, it's all working great.