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
Let's go over the User Stories to build a shopping list application
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
[MUSIC]
0:00
All right,
we've now got lists in our bag of tricks.
0:04
We can start to make applications
a lot more interactive.
0:07
Instead of just prompting for a single
response, we can continuously ask for
0:10
more information and store it in a list.
0:14
There are a lot of applications
that build lists, but
0:17
one that I'm very familiar with is one
that builds my weekly shopping list.
0:19
I love these things.
0:23
I've got one on my phone, I've got a to
do list app that I use on my Mac, and
0:24
I even use an Alexa to
add stuff to my list.
0:28
I can't get enough,
let's make another one.
0:30
I've already built the user stories for
it.
0:32
All right, let's take a look at
the to do list for the project.
0:34
I put these into user stories and
I'm using Trello here.
0:40
Again, it's another list.
0:43
I told you I love these lists.
0:44
But here we go.
So as a user, I should continually be
0:46
prompted so that I can add a grocery
item or view my list when I need to.
0:49
So we wanna just keep on asking,
hey what do you wanna buy?
0:53
What do you wanna buy?
0:55
That sounds good.
0:56
Put that into to do, we can do that.
0:57
As a user,
I should be able to ask for help so
0:59
that I can understand how
to use the application.
1:01
Also always a great idea,
glad there's a ticket for that.
1:04
As a user, I should be able
to add an item for the list.
1:08
What good is one of these list apps
without being able to add an item, right?
1:11
So as a user upon adding
an item to a list,
1:14
I should know the total length
of my list so I don't go over.
1:18
So that I don't, I'll say, let's fix that.
1:23
So I don't over buy,
change this real quick.
1:25
So we don't over buy or
over purchase there, there we go.
1:28
As a user, I should be able to
see the list at any time so
1:33
that I can verify my order.
1:36
Yeah, we want to be able
to look at the list and
1:38
make sure that that's
what you got going on.
1:40
And then finally, as a user I should
be able to state when I'm done so
1:42
that I can print out the list in totality,
that makes sense.
1:46
Okay, so let's go ahead,
let's pick up this first one,
1:50
let's get our loop going, right?
1:53
So I should continually be prompted so
that I can add a grocery item or
1:55
view my list when I need to.
1:59
I wanna move that to In Progress.
2:00
All right, let's flip over here.
2:04
Let's go ahead and
we're gonna add a new file,
2:06
we will call it shopping_list.py.
2:11
So we want some sort of main loop,
don't we?
2:14
There are a couple of different
commands that we need to handle.
2:18
Now, I saw one that wants
us to show the list, and
2:20
there's one where you can ask for help.
2:22
And then there is another one that
lets you say when you're done.
2:24
So we've got to handle all these commands.
2:27
When you want a loop to run for
an indefinite amount of time,
2:29
one trick that you can use is what
is known as an infinite loop.
2:32
Now it's probably gonna feel a little
weird, but what you can do is this.
2:36
You can say while True.
2:40
Now what's strange about that is that it
would appear that whatever the body of
2:43
this loop is gonna be,
it's gonna run forever, right?
2:48
This is always gonna be true,
true's never gonna not be true.
2:51
So this loop will run forever
unless someone interrupts it.
2:55
Well, lucky for us you can do that.
3:00
There's a keyword named break, and
3:02
what it does is it breaks
out of the outermost loop.
3:05
So let's do this, let's go ahead and
our first line here,
3:08
let's take some input from the user.
3:12
I'll just call this,
new_item, and I'll say input,
3:14
and we'll make it generic, right?
3:18
Cuz they're either gonna
be typing an item,
3:21
or they're gonna be typing one of
those commands like HELP or DONE.
3:23
So we'll make this generic,
we'll do a greater than sign there.
3:27
All right, so let's take a look really
quick at our stories, prompt this so
3:31
that I can add a grocery item,
review my list when I needed to.
3:35
It's almost done.
3:39
Let's go ahead and grab, this one here is
a good one to look at because this will
3:40
help us show how to break out of
this infinite loop we just created.
3:44
Let's get that so we can do that.
3:47
So as a user, I should be able
to state when I am done so
3:48
that I can print my list in totality.
3:50
Let's bring that up here to
In Progress and come back.
3:52
What we wanna have happen is,
3:56
if the user inputs DONE in all
capital letters we'll exit the loop.
3:58
So let's use that break keyword,
we can do that, right?
4:03
So we say if new_item = DONE and
we're gonna use all caps there.
4:05
What we want to have happen is we
want to break out of the loop.
4:13
Cool, so let's give that a run,
let's just make sure that that's working.
4:17
So if we say python shopping_list,
4:20
we can say anything at all,
anything we wanna say here.
4:23
But when we type DONE, boom the program
exits because we broke out of the loop.
4:27
This loop was just running and running and
running but when we said break,
4:32
we dropped out.
4:36
Awesome, so
4:37
how are our users going to know that they
need to type DONE in all caps like that?
4:38
Well, let's look at that list again,
let's look here.
4:44
Here it is, HELP, as a user I
should know how to ask for help so
4:47
that I can understand how
to use the application.
4:50
So good that that's there,
let's do that one too.
4:52
Let's come back over.
4:55
So I could assume that we'll probably
want to show HELP before the program
4:57
starts, right?
5:01
So you can kind of see what's happening.
5:02
And the ticket says that they
should be able to ask for
5:05
help whenever they want to so
that's also one of these commands, right?
5:07
So let's do this, let's make a function so
that we can call it when we need it.
5:11
So I'm gonna come up here and I'm gonna
give us some space and say def show help.
5:16
That makes sense and
I we'll describe, I'll say print.
5:23
So we'll show this one
when they first see it.
5:28
What should we pick up at the store?
5:31
And I'm going to use
a multi line string here.
5:37
And this is really good I think for
when you wanna add some help.
5:40
So we'll come over here, so
remember, you can do multiple lines.
5:43
So we'll say Enter DONE
to stop adding items,
5:46
and Enter HELP for this help,
5:52
this help was shown right here.
5:56
So then I'll close that off, and
I will close the print statement.
6:00
Awesome, and we'll call it, right,
when the program starts, right?
6:04
Why not, we'll say, show_help.
6:07
We'll call that and we also want to show
it if our user types that command, help.
6:10
So I guess, elif new_item = HELP, so
6:16
if they do = HELP, we'll do show_help.
6:20
And we don't really want
the rest of this loop to run,
6:26
no matter what's happening down here.
6:31
We have completed what we need to do for
this thing on the loop.
6:35
So one thing that we can do,
which is along the same lines of this
6:39
break keyword,
is to use the continue keyword.
6:43
So continue makes the code jump back
to the while statement immediately,
6:46
like none of the rest of
the code in the loop will run.
6:50
It will jump right back up to
the while true line there.
6:53
So break breaks out of the loop, and
continue completes the current iteration.
6:56
So I think we should add it here, right?
7:01
Since we want it to come back to
the prompt the soonest help is shown,
7:03
we don't really know what's
gonna happen below this.
7:07
But the job for this specific
iteration of the loop is done already.
7:10
So let's go ahead, and let's say continue.
7:14
Awesome, cool, so we'll do clear and we'll
give this a run, Python shopping list.
7:16
There is our help,
that looks pretty good doesn't it?
7:23
So we can say blah,
blah and I can type HELP.
7:25
Hey, there's our help again,
and I can type DONE.
7:30
There we go, and with that let's move
some of these tickets over Done.
7:32
So continually prompted
definitely happening.
7:36
As a user, I should be able
to state when I am done so
7:40
that I can print out the list in totality.
7:42
Let's leave that one running so
we can remember to print out the list.
7:44
Cuz there's a little bit
of a gotcha in there and
7:47
we wanna show the list when we're done.
7:49
But we don't even have a list yet.
7:50
So I'm gonna move this one over too,
ask for help, so that you can use it.
7:52
We're increasing, and we're gonna keep
7:56
this Done ticket alive because we haven't
even created the actual list yet.
7:58
Let's change that right
after this quick break.
8:03
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