Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
We can start to get into the actual behavior of our application now. We'll use an OrderedDict
to hold onto our choices and a nifty trick to print out the options to the user and then run their selected function.
New terms
-
OrderedDict
- a handy container from thecollections
module that works like adict
but maintains the order that keys are added -
.__doc__
- a magic variable that holds the docstring of a function, method, or class
-
0:00
If you've programmed in other languages,
-
0:01
you've probably come across something called a switch.
-
0:04
A switch, is kinda like an IF conditional.
-
0:06
But it has multiple possibilities or cases as they're often called.
-
0:09
Python doesn't have switches.
-
0:11
And this is an intentional omission.
-
0:13
Switches, are often a source of bugs and can be really confusing for people.
-
0:17
We can, however, use a dictionary as a list of triggers and their actions.
-
0:21
Dictionaries though, as you remember, don't have an order.
-
0:24
No one wants a menu that keeps moving around them.
-
0:27
So, I'm gonna introduce you to a handy tool known as an ordered dict.
-
0:31
Every time we've built a menu so far, it's been, pretty simple, just one or
-
0:35
two choices that we've been presenting to the users,
-
0:37
as a string inside of the input function.
-
0:40
Well, I want this to be more like an application, as I've been talking about.
-
0:44
So, I wanna menu that looks more, professional.
-
0:48
Let's start by deciding our options.
-
0:51
So, what we're gonna do, is from collections, import OrderedDict.
-
0:59
And collections, if you haven't looked at it,
-
1:02
is a great part of the standard library that holds a lot of extra container types.
-
1:06
There are things like order to dict, and named tupple, which extend some built ins,
-
1:10
dictionaries and tupples, or there's like, whole new things, like counter, which
-
1:16
kinda looks and acts like a dictionary but doesn't work like a dictionary.
-
1:19
And way worth checking out.
-
1:21
Check all of it out, actually, whenever you get a chance.
-
1:24
We're gonna use ordered Dict, to keep the options in the order that we want.
-
1:29
So we can define our menu over one, let's just do it right up here.
-
1:33
So ordered Dict and so ordered Dict,
-
1:39
unlike regular dictionaries, remembers the order things are added.
-
1:42
The way it does this, is we add things as a list.
-
1:46
Which is kinda cool, in its own little way.
-
1:51
So, let's add our items.
-
1:54
Each one of our items,
-
1:55
is a tupple which actually a pretty common way of building a dictionary.
-
1:58
Play around with that, see if you can figure it out.
-
2:00
And what we're gonna do, is we're gonna use a letter.
-
2:02
So we're gonna say the letter A, is for add entry.
-
2:07
And we're gonna say the letter V is for, oops,
-
2:11
not quotes, is for view entry or entries.
-
2:15
So only two things in here, so far.
-
2:17
But, you know,
-
2:18
I'm pretty sure we're gonna add something else before the course is over.
-
2:22
The oder dic behaves just like a normal dictionary.
-
2:24
The only difference, as I said, is that it remembers the order that the keys went in.
-
2:29
They always come back out, in that same order.
-
2:31
So let's come down here to our menu loop function and let's use our menu.
-
2:38
Let's show the menu and handle whenever the user makes a choice.
-
2:43
All right so let's say choice is equal to none.
-
2:47
That's just you know, our way of setting a variable without giving it a value and
-
2:52
then let's say while choice, does not equal q let's
-
3:00
actually say while choice, yeah no that's fine.
-
3:04
So, while choice doesn't equal q.
-
3:06
We're gonna print out enter q to quit.
-
3:12
So we give them a nice, easy way of quitting.
-
3:17
So then, let's say for
-
3:18
key value, in menu.items, cuz remember, menu.items gives us a tuple.
-
3:24
We wanna get the key and the value.
-
3:27
We're gonna print, a string which has a placeholder.
-
3:32
And a parentheses, and another placeholder.
-
3:35
And we're gonna format that with the key, and value.dunder doc.
-
3:40
Talk about what that one is here in just a minute.
-
3:43
And then we're gonna say, choice, is equal to input.
-
3:50
That's the action and then lets lower case that and strip off any extra spaces.
-
3:58
Then lets do if choice in menu.
-
4:03
And then we're gonna do menu, choice.
-
4:07
All right, there's a lot going on here.
-
4:09
Let's break this down line by line.
-
4:13
So, first, like I said, we're setting our choice variable.
-
4:17
None is a pretty good and really,
-
4:19
really common way, of initializing a variable without giving it a value.
-
4:22
So, like in JavaScript, you know, you can do,
-
4:24
you know, var a, and suddenly you have a variable called a that has no value.
-
4:28
You can't do that in Python, so you set things equal to none.
-
4:32
Then, so long as they haven't chosen q as their choice, we're
-
4:36
gonna print out a message saying that hey you can put in q in order to quit.
-
4:40
Then, we're gonna loop through each of the items in our dictionary, the key and
-
4:44
the value and we're gonna print out the key and the value so
-
4:47
we'll get like A, and then something, that's where this value.dot comes in.
-
4:51
So what is value dot, doc?
-
4:53
Well, value is gonna be a function, right?
-
4:57
Because our keys, our functions, so add entry, sorry not our keys, our values.
-
5:03
Add entry is a value.
-
5:05
Add entry, is a function it's this function.
-
5:08
And this dunder doc is the doc string for that function.
-
5:13
So it's gonna say a, closing parentheses, add an entry.
-
5:17
That's kinda cool.
-
5:18
We're gonna capture their choice,
-
5:19
we're gonna let them type in whatever choice they want.
-
5:21
We're gonna lower case it and strip it.
-
5:23
We check to see if it's q, if it's q we're gonna quit.
-
5:25
If it's not q.
-
5:27
Then we come back to our menu,
-
5:30
we find the function that they've selected, and we run it.
-
5:33
I know there's a lot there, but it's all just laid out, one thing after the next.
-
5:39
Feel free to go in and add comments and, and all sorts of stuff here.
-
5:43
Just make sure you understand what's going on.
-
5:47
Let's give this a try, 'kay?
-
5:50
So we're gonna come back down here,./diary.
-
5:56
And if we look at this we get an error.
-
5:58
I said that it didn't matter where we put our menu, and it does matter.
-
6:02
So let's cut our menu out of there, and let's put our menu.
-
6:09
Right down here.
-
6:10
[BLANK_AUDIO]
-
6:14
All right.
-
6:15
Save that.
-
6:17
Sometimes these things happen.
-
6:19
There we go. There's our menu.
-
6:20
So it says enter q to quit, a to add an entry, v to view previous entries.
-
6:25
If I hit a.
-
6:28
It actually just comes right back, cause our function doesn't do anything.
-
6:30
I just hit 'V'.
-
6:32
Same thing.
-
6:33
Alright, I should be able to hit 'Q' in order to quit, and I quit.
-
6:37
Awesome.
-
6:38
We get a really nice looking menu.
-
6:39
I, I think it looks really nice.
-
6:42
We can choose our different actions.
-
6:43
They don't do anything right now, but we can choose them, and we can quit with 'Q'.
-
6:47
I think that's a great start.
-
6:49
So what do you think?
-
6:50
Is order dict a good way to deal with a menu?
-
6:52
I don't normally like using a dictionary as an alternate switch.
-
6:55
But I think they're a great idea in this use case.
-
6:58
It's clean, it's concise and it's easily extended.
-
7:00
Three of my favorite things.
You need to sign up for Treehouse in order to download course files.
Sign up