Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Import the CSV of books to create your first entries.
This video doesn't have any notes.
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
With our database ready to go, we need
to start building out our application.
0:00
Let's start with the menu function.
0:04
It'll print out the menu options
to the console, ask the user for
0:06
their choice, and then return their
choice for use in our main app function.
0:10
Couple spaces.
0:20
def menu and then inside we're
going to create a while loop.
0:23
Do while True.
0:31
And then let's print out our menu options.
0:34
Print, two, three, four, five, six, and
0:37
I'm doing the triple so
that we can do multi-line here.
0:40
And let's do, I'm gonna do caps lock,
PROGRAMMING BOOKS.
0:46
1 is going to be Add book.
0:55
2 is going to be View all books.
0:59
3, Search for book.
1:03
4, Book analysis,
1:08
and 5, Exit.
1:13
Great, and let's do the input, too.
1:16
So let's do input and let's just say,
1:20
What would you like to do?
1:26
And that space, okay, don't worry,
we'll capture it in a second.
1:31
But let's come down here to the bottom.
1:35
And let's call our menu function.
1:38
And let's give it a look in the console.
1:42
python3 app.py, cool, and
1:46
you can see our input is
starting over here on the left.
1:49
But all of this is getting our
indent that's coming from the indent
1:55
here to be inside of our function.
2:00
And inside of our while loop and
inside of our print function call.
2:02
So to counteract that, I'm going to say,
let's start you on a new line with a \n.
2:06
And then for the rest of these,
we'll do returns.
2:13
It's like hitting the Return or
Enter key on your keyboard.
2:16
So \ return, \return, \return,
2:19
\return, \return, okay, save.
2:23
And then to cancel this, we're gonna hit
Ctrl+C, and that'll cancel your program.
2:27
I'm just gonna run clear.
2:34
And then python3 app.py again.
2:36
Cool, okay, that looks much better.
2:41
It's over here on the left and
it looks nice and neat.
2:43
I'm gonna go ahead and
hit Ctrl+C and clear it again.
2:46
Okay, awesome, now we need to ask for
the user's input and
2:50
we need to save it to a variable.
2:54
So we have the ask,
let's save it to a variable called choice.
2:56
And we also need to make sure that
the choice they give us is actually one of
3:01
the available options.
3:06
So let's do, if choice in, if it's in, and
3:09
let's just do a list of our
options because it's quite short.
3:13
So 1, 2, 3, 4, 5, so
3:19
if it's one of these options,
3:23
then we can return the choice.
3:28
And if it's not, it'll just cause
the loop to go back up top and
3:35
print out our programming books again.
3:38
And it will try again.
3:41
But to make it a little bit easier
on the user, let's also add an else.
3:45
And then let's ask them to try again.
3:52
That way they know that
they've made a mistake.
3:54
So I'm gonna do, Please choose
3:57
one of the options above.
4:02
And I'm actually gonna make this
triple quotes here, one, two,
4:06
three, let's go to the end, two, three.
4:11
Okay, then I can do this.
4:15
Please choose one of the options above.
4:17
A number from 1-5.
4:22
Press enter to try again.
4:28
Okay, this way they receive some sort of
message before it kicks them back up to
4:31
the start of the loop.
4:35
Otherwise, they might think they
entered something correctly,
4:37
had a little keyboard flub.
4:40
And they thought they pushed 1 and
really pushed the key next to 1,
4:41
that little tilde symbol.
4:45
And instead of just seeing
the menu again and being like,
4:46
wow, your program is broken.
4:50
They'll get a nice little error
message and realize, oops,
4:52
they must have typed something
incorrectly and they can try again.
4:54
Okay, so
let's test this out in the console.
4:57
So what would you like to do?
5:01
Let's try just a letter, t.
5:02
Please choose one of the options above.
5:04
And you can see it's
getting this spacing again.
5:06
So I'm gonna do, I'm gonna try doing \r.
5:09
Save that, okay, but we can still test.
5:16
Let's just try doing 14,
a number that's not in our range, and
5:18
we still get the error, perfect.
5:23
If we do one of the correct numbers,
it should return a choice,
5:26
which we're not capturing.
5:29
So it should just end our function,
perfect.
5:31
That's the way it's supposed to work.
5:35
And I'm gonna run it one more time just
to make sure that error is aligned
5:36
a bit nicer.
5:39
So I'm just do the letter t, there we go.
5:40
Okay, that's nicely aligned over there.
5:43
Cool, keyboard interrupt, clear.
5:45
And our menu function's good to go.
5:50
And it's important to note, the reason
why I'm using while True here is because
5:54
no matter what, this function will
only end when this choice is returned.
5:58
And return will always cancel out or
stop a loop.
6:02
So that's why I used while True.
6:07
Next, let's create our app function.
6:10
Scroll down here to the bottom because
it's probably going to use a lot of
6:13
the functions we're gonna be creating.
6:17
And so this function will need to go at
the end right here before the dunder main.
6:19
Add a couple spaces, and def app.
6:24
Inside of our function, we'll need
a while loop to control our app,
6:29
kind of like what we did
with the menu up above.
6:33
So that when the file is run, the loop
will continue to let the user interact
6:36
with the database until they
decide they wanna exit.
6:40
Let's create a variable called
app_running and set it equal to True.
6:43
And then let's do while app_running,
perfect.
6:49
The first thing we want our users
to see is that main menu so
6:57
that they know what options they have.
7:00
So we can call the menu.
7:03
And remember the menu, let me scroll up,
is going to return their choice.
7:05
So we wanna capture that by
setting menu equal to a variable.
7:11
And we can just call it choice so
that it's still their menu choice.
7:16
Now that we have it, we can use if
statements to control what happens next.
7:22
So if choice is equal to 1 then
that means from our menu above.
7:26
And I'll scroll again, so
you all can see, we have 1 is Add book.
7:32
So I'm gonna do 1 and I'm gonna do
a little note here, this is add book.
7:40
And I'm just gonna put pass inside for
7:44
right now cuz we'll add
all this stuff later.
7:50
And then elif choice equals 2,
this was view books, pass.
7:56
Elif choice equals 3 and
8:03
then this was search for book.
8:07
You need to put in pass.
8:14
And then we need elif choice equals 4 and
8:16
this is the book analysis, pass.
8:22
And here we can just use else because
our menu function is already taking
8:29
care of any other numbers
besides one through five.
8:33
So we know we're only gonna get
numbers one through five or,
8:36
I guess, string numbers one through five.
8:39
So we can just use else and
8:42
then we can put GOODBYE, Print, GOODBYE.
8:45
And then we can set app_running
equal to False to turn off our loop.
8:51
Now inside of dunder main, let me save.
8:59
We don't need to call menu anymore.
9:02
Instead, we can just call our app.
9:04
Let's run the file.
9:06
And awesome,
our menu's getting printed out still.
9:10
And now we can test out
all of our menu options.
9:13
So if I type in a letter,
I should still get an error.
9:17
Awesome, I get the menu again.
9:21
If I type in 12, that's not an option,
still get the error, perfect.
9:24
If I type in 1, because we have nothing
in any of these choices, except for
9:30
our Exit choice, it should just cause
the while loop to start back up the top.
9:36
So if I do 1 I still get the menu,
2, 3, 4, and
9:42
now when I hit 5,
it should close this out.
9:46
And there we go, perfect,
it's all working great.
9:51
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