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 get that list populated!
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
All right, so what's next?
0:00
Let's get that list created.
0:02
I'm gonna pull over this.
0:03
As a user,
I should be able to add an item to a list.
0:05
Let's do it.
0:08
So in our shopping list here I
think right out of the gate,
0:13
we should Create a new empty
list named shopping_list.
0:18
Let's do that.
0:24
You know how to do that and
then after we make
0:25
that new list,
we should make a new function.
0:30
We should Create the function named
add_to_list that declares a parameter,
0:35
Named item.
0:48
And then inside the body of that just go
ahead and say Add the item to the list.
0:51
Awesome, and then finally let's go in and
we'll call that function right about here.
1:01
We'll go down here.
1:07
We'll call that function, so
we'll say Call add_to_list
1:09
with new_item as an argument.
1:14
All right, that make sense?
1:19
We're going to come up here, create
an empty list, we'll create a function,
1:22
we'll add the item to the list.
1:26
Makes sense, right?
1:27
You got this.
1:29
So here, get ready to pause me and
you give those a go, one at a time.
1:30
Then I'll let you know before I start
the next one if you wanna peek after
1:35
each step.
1:39
But I know you can do this, ready?
1:39
Pause me.
1:42
So how'd you do?
1:44
So creating an empty list looks like this.
1:45
You say shopping_list = [].
1:47
All right, and now for the function.
1:53
So we are going to define a new
function named add_to_list.
1:55
And it declares a parameter name item.
2:01
So there we are and
there's a colon to get to the body.
2:04
I'm gonna add the item to our list.
2:08
So to add to the end of a list,
you append, right?
2:11
So we're gonna say shopping_list.append
and we're gonna pass in that item.
2:15
Awesome, and finally, let's call it.
2:21
So we will say down here,
let's call that add_to_list.
2:24
Basically, anything that's come through
here, and made it down this far,
2:31
we're just gonna add it to the list.
2:35
Sounds good, and since we are doing
of all of our checks here,
2:37
we've got a thing set up.
2:41
We're pretty much protected from
commands making their way through.
2:42
As long as we are diligent
about using break and continue,
2:46
this will only be proper items.
2:51
It seems like it's working, but we don't
have a way of showing the list just yet.
2:54
But let's see,
2:58
is there something else on the board
we can tackle before we get there?
2:59
As a user, upon adding an item to a list,
I should know the total length of my list,
3:03
so that I don't go buy.
3:08
Boy, I didn't edit that very good.
3:10
I don't over buy is what
I want that to say.
3:11
So that I don't over buy, this is
important part of the communication there.
3:14
So that I don't overpurchase is
probably a better word than that even.
3:20
All right, so as a user, upon adding
an item to a list, I should know the total
3:23
length of the list so that you're
there's a 100 list things on this.
3:26
We don't want that.
3:29
So let's do that.
3:29
So let's just put that in the add_to_list,
right?
3:31
If we come in here,
right about here, right?
3:35
Don't you think?
So we'll say Notify the user that the item
3:38
was added, and state the number
of items in the list currently.
3:44
We don't want them to over buy,
[LAUGH] over purchase, got it?
3:52
All right, pause me, go for it.
3:58
How'd you do?
4:01
Here's how I did.
4:03
So I said print.
4:03
And then I said Added: List has {} items.
4:05
Format and we are going to get the number
of items by passing it to len.
4:12
Here we go.
4:19
Did I close them all out?
4:21
There we go.
4:23
Awesome, so let's give that a run.
4:25
We'll say python shopping_list.
4:27
And I wanna pick up some apples.
4:31
1 items, a common bug.
4:33
And we'll do bananas, and now we can
forget about it because we got 2 items,
4:37
awesome.
4:41
But the items are adding, so I think we've
got some tickets to complete, don't you?
4:42
So I should be able to add items to list.
4:47
Let's go ahead and do that.
4:49
And as a user, upon adding an item,
we know how many are there now.
4:51
Awesome, and
I'm gonna type DONE because I can, DONE.
4:55
Let's knock out those last few
issues right after this quick break.
5: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