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