1 00:00:00,170 --> 00:00:01,720 If you've programmed in other languages, 2 00:00:01,720 --> 00:00:04,460 you've probably come across something called a switch. 3 00:00:04,460 --> 00:00:06,200 A switch, is kinda like an IF conditional. 4 00:00:06,200 --> 00:00:09,930 But it has multiple possibilities or cases as they're often called. 5 00:00:09,930 --> 00:00:11,350 Python doesn't have switches. 6 00:00:11,350 --> 00:00:13,180 And this is an intentional omission. 7 00:00:13,180 --> 00:00:17,320 Switches, are often a source of bugs and can be really confusing for people. 8 00:00:17,320 --> 00:00:21,850 We can, however, use a dictionary as a list of triggers and their actions. 9 00:00:21,850 --> 00:00:24,840 Dictionaries though, as you remember, don't have an order. 10 00:00:24,840 --> 00:00:27,090 No one wants a menu that keeps moving around them. 11 00:00:27,090 --> 00:00:31,020 So, I'm gonna introduce you to a handy tool known as an ordered dict. 12 00:00:31,020 --> 00:00:35,000 Every time we've built a menu so far, it's been, pretty simple, just one or 13 00:00:35,000 --> 00:00:37,550 two choices that we've been presenting to the users, 14 00:00:37,550 --> 00:00:40,680 as a string inside of the input function. 15 00:00:40,680 --> 00:00:44,540 Well, I want this to be more like an application, as I've been talking about. 16 00:00:44,540 --> 00:00:48,460 So, I wanna menu that looks more, professional. 17 00:00:48,460 --> 00:00:50,760 Let's start by deciding our options. 18 00:00:51,920 --> 00:00:58,430 So, what we're gonna do, is from collections, import OrderedDict. 19 00:00:59,690 --> 00:01:02,010 And collections, if you haven't looked at it, 20 00:01:02,010 --> 00:01:06,641 is a great part of the standard library that holds a lot of extra container types. 21 00:01:06,641 --> 00:01:10,750 There are things like order to dict, and named tuple, which extend some built ins, 22 00:01:10,750 --> 00:01:16,320 dictionaries and tuples, or there's like, whole new things, like counter, which 23 00:01:16,320 --> 00:01:19,430 kinda looks and acts like a dictionary but doesn't work like a dictionary. 24 00:01:19,430 --> 00:01:21,720 And way worth checking out. 25 00:01:21,720 --> 00:01:24,520 Check all of it out, actually, whenever you get a chance. 26 00:01:24,520 --> 00:01:28,460 We're gonna use ordered Dict, to keep the options in the order that we want. 27 00:01:29,520 --> 00:01:33,890 So we can define our menu over one, let's just do it right up here. 28 00:01:33,890 --> 00:01:39,090 So ordered Dict and so ordered Dict, 29 00:01:39,090 --> 00:01:42,410 unlike regular dictionaries, remembers the order things are added. 30 00:01:42,410 --> 00:01:45,370 The way it does this, is we add things as a list. 31 00:01:46,760 --> 00:01:49,460 Which is kinda cool, in its own little way. 32 00:01:51,710 --> 00:01:54,280 So, let's add our items. 33 00:01:54,280 --> 00:01:55,200 Each one of our items, 34 00:01:55,200 --> 00:01:58,630 is a tuple which actually a pretty common way of building a dictionary. 35 00:01:58,630 --> 00:02:00,590 Play around with that, see if you can figure it out. 36 00:02:00,590 --> 00:02:02,280 And what we're gonna do, is we're gonna use a letter. 37 00:02:02,280 --> 00:02:05,570 So we're gonna say the letter A, is for add entry. 38 00:02:07,110 --> 00:02:11,450 And we're gonna say the letter V is for, oops, 39 00:02:11,450 --> 00:02:15,880 not quotes, is for view entry or entries. 40 00:02:15,880 --> 00:02:17,870 So only two things in here, so far. 41 00:02:17,870 --> 00:02:18,970 But, you know, 42 00:02:18,970 --> 00:02:22,540 I'm pretty sure we're gonna add something else before the course is over. 43 00:02:22,540 --> 00:02:24,880 The OrderedDict behaves just like a normal dictionary. 44 00:02:24,880 --> 00:02:29,080 The only difference, as I said, is that it remembers the order that the keys went in. 45 00:02:29,080 --> 00:02:31,810 They always come back out, in that same order. 46 00:02:31,810 --> 00:02:38,720 So let's come down here to our menu loop function and let's use our menu. 47 00:02:38,720 --> 00:02:43,420 Let's show the menu and handle whenever the user makes a choice. 48 00:02:43,420 --> 00:02:45,500 All right so let's say choice is equal to none. 49 00:02:47,050 --> 00:02:52,040 That's just you know, our way of setting a variable without giving it a value and 50 00:02:52,040 --> 00:02:58,300 then let's say while choice, does not equal q let's 51 00:03:00,580 --> 00:03:04,290 actually say while choice, yeah no that's fine. 52 00:03:04,290 --> 00:03:06,350 So, while choice doesn't equal q. 53 00:03:06,350 --> 00:03:11,070 We're gonna print out enter q to quit. 54 00:03:12,940 --> 00:03:15,560 So we give them a nice, easy way of quitting. 55 00:03:17,100 --> 00:03:18,849 So then, let's say for 56 00:03:18,849 --> 00:03:24,490 key value, in menu.items, cuz remember, menu.items gives us a tuple. 57 00:03:24,490 --> 00:03:25,840 We wanna get the key and the value. 58 00:03:27,530 --> 00:03:32,440 We're gonna print, a string which has a placeholder. 59 00:03:32,440 --> 00:03:35,710 And a parentheses, and another placeholder. 60 00:03:35,710 --> 00:03:40,320 And we're gonna format that with the key, and value.dunder doc. 61 00:03:40,320 --> 00:03:43,080 Talk about what that one is here in just a minute. 62 00:03:43,080 --> 00:03:49,170 And then we're gonna say, choice, is equal to input. 63 00:03:50,500 --> 00:03:56,940 That's the action and then lets lower case that and strip off any extra spaces. 64 00:03:58,530 --> 00:04:02,120 Then lets do if choice in menu. 65 00:04:03,980 --> 00:04:06,820 And then we're gonna do menu, choice. 66 00:04:07,870 --> 00:04:09,500 All right, there's a lot going on here. 67 00:04:09,500 --> 00:04:13,200 Let's break this down line by line. 68 00:04:13,200 --> 00:04:17,340 So, first, like I said, we're setting our choice variable. 69 00:04:17,340 --> 00:04:19,080 None is a pretty good and really, 70 00:04:19,080 --> 00:04:22,670 really common way, of initializing a variable without giving it a value. 71 00:04:22,670 --> 00:04:24,990 So, like in JavaScript, you know, you can do, 72 00:04:24,990 --> 00:04:28,810 you know, var a, and suddenly you have a variable called a that has no value. 73 00:04:28,810 --> 00:04:32,010 You can't do that in Python, so you set things equal to none. 74 00:04:32,010 --> 00:04:36,390 Then, so long as they haven't chosen q as their choice, we're 75 00:04:36,390 --> 00:04:40,810 gonna print out a message saying that hey you can put in q in order to quit. 76 00:04:40,810 --> 00:04:44,720 Then, we're gonna loop through each of the items in our dictionary, the key and 77 00:04:44,720 --> 00:04:47,050 the value and we're gonna print out the key and the value so 78 00:04:47,050 --> 00:04:51,530 we'll get like A, and then something, that's where this value._doc_ comes in. 79 00:04:51,530 --> 00:04:53,560 So what is value._doc_? 80 00:04:53,560 --> 00:04:57,170 Well, value is gonna be a function, right? 81 00:04:57,170 --> 00:05:03,830 Because our keys, our functions, so add entry, sorry not our keys, our values. 82 00:05:03,830 --> 00:05:05,430 Add entry is a value. 83 00:05:05,430 --> 00:05:08,460 Add entry, is a function it's this function. 84 00:05:08,460 --> 00:05:13,320 And this dunder doc is the doc string for that function. 85 00:05:13,320 --> 00:05:17,430 So it's gonna say a, closing parentheses, add an entry. 86 00:05:17,430 --> 00:05:18,400 That's kinda cool. 87 00:05:18,400 --> 00:05:19,560 We're gonna capture their choice, 88 00:05:19,560 --> 00:05:21,480 we're gonna let them type in whatever choice they want. 89 00:05:21,480 --> 00:05:23,640 We're gonna lower case it and strip it. 90 00:05:23,640 --> 00:05:25,780 We check to see if it's q, if it's q we're gonna quit. 91 00:05:25,780 --> 00:05:27,410 If it's not q. 92 00:05:27,410 --> 00:05:28,900 Then we come back to our menu, 93 00:05:30,020 --> 00:05:33,950 we find the function that they've selected, and we run it. 94 00:05:33,950 --> 00:05:39,300 I know there's a lot there, but it's all just laid out, one thing after the next. 95 00:05:39,300 --> 00:05:43,540 Feel free to go in and add comments and, and all sorts of stuff here. 96 00:05:43,540 --> 00:05:45,920 Just make sure you understand what's going on. 97 00:05:47,010 --> 00:05:48,790 Let's give this a try, 'kay? 98 00:05:50,050 --> 00:05:56,660 So we're gonna come back down here,./diary. 99 00:05:56,660 --> 00:05:58,440 And if we look at this we get an error. 100 00:05:58,440 --> 00:06:01,160 I said that it didn't matter where we put our menu, and it does matter. 101 00:06:02,510 --> 00:06:07,430 So let's cut our menu out of there, and let's put our menu. 102 00:06:09,490 --> 00:06:10,131 Right down here. 103 00:06:10,131 --> 00:06:14,854 [BLANK_AUDIO] 104 00:06:14,854 --> 00:06:15,369 All right. 105 00:06:15,369 --> 00:06:17,160 Save that. 106 00:06:17,160 --> 00:06:18,210 Sometimes these things happen. 107 00:06:19,600 --> 00:06:20,690 There we go. There's our menu. 108 00:06:20,690 --> 00:06:25,210 So it says enter q to quit, a to add an entry, v to view previous entries. 109 00:06:25,210 --> 00:06:26,580 If I hit a. 110 00:06:28,080 --> 00:06:30,280 It actually just comes right back, cause our function doesn't do anything. 111 00:06:30,280 --> 00:06:32,500 I just hit 'V'. 112 00:06:32,500 --> 00:06:33,330 Same thing. 113 00:06:33,330 --> 00:06:37,320 Alright, I should be able to hit 'Q' in order to quit, and I quit. 114 00:06:37,320 --> 00:06:38,060 Awesome. 115 00:06:38,060 --> 00:06:39,910 We get a really nice looking menu. 116 00:06:39,910 --> 00:06:42,330 I, I think it looks really nice. 117 00:06:42,330 --> 00:06:43,670 We can choose our different actions. 118 00:06:43,670 --> 00:06:47,410 They don't do anything right now, but we can choose them, and we can quit with 'Q'. 119 00:06:47,410 --> 00:06:49,220 I think that's a great start. 120 00:06:49,220 --> 00:06:50,240 So what do you think? 121 00:06:50,240 --> 00:06:52,180 Is order dict a good way to deal with a menu? 122 00:06:52,180 --> 00:06:55,470 I don't normally like using a dictionary as an alternate switch. 123 00:06:55,470 --> 00:06:58,040 But I think they're a great idea in this use case. 124 00:06:58,040 --> 00:07:00,750 It's clean, it's concise and it's easily extended. 125 00:07:00,750 --> 00:07:01,830 Three of my favorite things.