Meal Planner Transcript
Start visual description. The instructor’s screen is shared where he shows how to prepare and write code for a particular program. He demonstrates the steps as he describes each aspect. The instructor can be seen in the top right-hand corner in a small box. End visual description.
[00:00:00]
Instructor: Hey! Let’s walk through the meal planner activity. So you want to
build a program that builds up a meal plan for several days. And an individual
meal needs to have a grain, a vegetable, and a fruit, and the user can specify as
many meals as they want and then we print out the planned meals.
[00:00:17]
Here’s some example output on the side for what we would want our program to
look like. Plan the meal, get the grain, vegetable, and fruit, plan another meal
with the grain, vegetable, fruit, et cetera.
[00:00:27]
Well, when they’re done, they’re going to hit return on the grain to say we’re
finished and then we’ll print out your three meals and the things that we want
there. Great.
[00:00:39]
Excuse me. So what are the big pieces? Today, I saw a great demonstration of
that in class, right? Some students were working together on this problem and
weren’t quite sure how to do it, and so one of them was like, well, we’re going to
get the meals and then we’re going to print the meals, your turn, and pass it off
to the next one. And then he had to figure out, well, I’m going to create a list and
I’m going to fill that list. Your turn and on down the line.
[00:01:06]
But that’s exactly the process that you follow as you decompose a problem.
What are the easy parts of the problem? Well, do that much. What’s the easy
part of that problem? Do that much and slowly whittle the problem down until it
is something manageable and easy that you can deal with.
[00:01:22]
So let’s do the same thing. Here, we can see we’ve got the get the meals part and
the print the meals part. So without further ado, let’s come over to meal planner
and do that much. Meals equals get meals and then we’re going to say print
meals. And now we can go in and start to understand what does it mean to get
the meals.
[00:01:47]
So let’s create a function, get meals, and we’ve seen this. So if you read through
the guide, you’ll see patterns and I want you to use those patterns. That’s why
they’re there is they’re useful and intended.
[00:02:03]
And so you might say, oh, we’ve seen this process before where I’m going to get
something and print something. That’s the registration activity that was already
laid out.
[00:02:13]
So I can open up registration, that pie here, and look at this registered
participants function, right? And I’ve got people and participant equals
participant. This is essentially, meals is empty, while true, meals equals get meal,
if meal is none, break, meals dot append meal, return meals.
[00:02:31]
In fact, that’s so exactly what we want. I’m just going to copy it and paste it. And
then I’ll change these variables. So I can come in here, right click, refactor,
rename. Let’s call this meals. And then this one. Refactor, rename, call that meal.
And then we’ll just change this to get meal, an individual meal. And essentially
how we want to do.
[00:03:00]
We’re going to delegate the work for what it means to actually get the details
about a meal to another function. But this function handles this idea that I need
to do that over and over and over and accumulate the results into one big list
and then return that list.
[00:03:18]
So what does it mean to get a meal? Well, once again, we can look back at
registration as we’ve been following that same pattern. What does that look like?
[00:03:27]
And so we’re going to have like a print for some instruction, we’re going to grab
the pieces. We’re going to check if the first piece was an empty string and if it is,
we return none. Otherwise we get everything else and return it all together.
[00:03:41]
So over here, we can follow a similar pattern, right? You can say print, plan a
meal. Is that what we had said here? Yeah, plan a meal. All right, let’s plan a
meal. And then we can say we want grain, vegetable, fruit. So, grain, veggie, fruit.
Grain equals input grain.
[00:04:06]
And then if grain is equal to empty string, return none. Returning none is what I
was saying. Return nothing. I don’t have anything. And down here, if the meal we
got back is nothing, then we’re done. We don’t append that nothing to our list.
And so that’s the way of communicating from one function back to the other. I
don’t have any information. The user stopped. We’re done.
[00:04:32]
But if that wasn’t the case, it’s a grain, so we’ve got the grain, we can continue.
We can say veggie equals input vegetables, and then fruit equals input fruit, and
then we can return grain, veggie, fruit out.
[00:04:55]
So the moment we say, we put commas here in the return statement, we’re
saying, hey, return a tuple of information. We’ve conceptually named this idea a
meal. A meal is comprised of a grain, a veggie, and a fruit. So return that meal
out.
[00:05:14]
And now the color of this function, you know, can capture that and so we’re
going to refer to it here as the box, the meal itself. Tuples are just like boxes,
right? A box shows up on your doorstep from Amazon, you open it up, and
there’s things inside. But Amazon doesn’t think about the things inside when
they’re shipping it. The postman doesn’t care about that. It’s just a box with stuff
in it.
[00:05:45]
So, here in this function, we’re thinking about the box at the meal level. It’s just a
meal. Is it none or do I keep it? Later on in the code, we’re going to unpack that
box. We’re going to unpack that meal and see what the individual pieces are so
we can do something with it. But here we’ll keep it just as a tuple.
[00:06:07]
So we can get a meal. We can get all the meals. We’ve got our meals. Here’s a list
now of tuples. So we’re going to print that out. Let’s print our meals. Create a
function, for meal in meals.
[00:06:21]
How do I unpack that box? That meal is actually composed of other pieces, so I
want those pieces, but I can say grain, veggie, fruit equals meal. And now I can
print it. Print, format string, grain, veggie, fruit. I don’t know if that was the order.
[00:06:59]
I had had a typo, but that’ll work for now. Veggie, fruit and then bring it up and
that’s the gist of it. Let’s run this. In fact, let’s run this with the debugger and let’s
look at some of the pieces, what’s going on in here.
[00:07:15]
So, I’ll put a break point right here and break point right there, just so we can
pause it. And I’ll come down here and let’s debug meal planner. Plan a meal. And
it’s asking for a grain. All right, let’s say bread. It wants a vegetable. Carrot. It
wants a fruit: banana.
[00:07:43]
Great. Now it pauses because it hit that break point and it’s ready to turn the
grain, the veggie, and the fruit, which is what we’ve got right here. So as we
return from this function, here’s the return value. Get meal has just returned a
tuple that says bread, carrot, banana. And it’s about to assign that to meal, so we
step. So now I have an individual meal. It’s a bread carrot and banana tuple.
[00:08:12]
And is that meal none? No. So it’s going to append it to our list. And now meals is
a list of one item. Here’s the brackets on the outside. That one item is a tuple
which itself has individual pieces, but the list doesn’t care. The list thinks about it
as simply a box. I have one box. Now we can look inside the box itself and see,
oh, the box has three things in it.
[00:08:38]
Great. Now it’s going to ask for get a meal again. And if we run that, so it’s going
to say give me a grain: rice. Give me a vegetable: tomato. Give me a fruit:
tomato. Sure, you can have that argument if you want. And so it’s about to return
now, tomato, rice, tomato. The grain, the veggie, and the fruit.
[00:09:03]
And so we can return and we can see that the return value is about to return, it’s
returning rice, tomato, tomato. We’re going to store that as meal. There it is.
That’s not none, so we’re going to append it, and now meals has two items in it.
It has a tuple that’s bread, carrot, banana, and a tuple that’s rice, tomato,
tomato. And then so we can build up a little bit more.
[00:09:28]
Let’s take that break print off and run this. So it’s going to ask for another grain.
We’ll say bread. Vegetable: peanut butter. Fruit: jam. And now let’s quit. So I hit
enter for grain. And which breaks, it returns a none here which returns meal says
no. So it breaks and returns meals. Meals comes out here. We go into print meals
and now we’ve hit this other break point and it stopped.
[00:10:02]
What do I have? I have meals. It’s a list of three things, three individual tuples. I
can list them out this way. So here’s the 1st, 2nd, and 3rd items in that list. And
we’re going to iterate over it. So if I go next, it grabs meals, the first one, a bread,
carrot and banana. I can unpack it now. And so grain, fruit and veggie. Now
they’re individual pieces so I can run it.
[00:10:27]
Moves back up, grabs the next meal. Meal is now rice, tomato, tomato. I can
unpack it, et cetera, and iterate through each one of those. So we hit run, turn
off that break point, and finish playing. And then it’ll print everything out for us.
[00:10:46]
You will often see, so here I’ve shown meal and in meals and then unpack it. You
can also just directly unpack it and I tend to write code that looks like this
instead. For grain, veggie, fruit in meals, this chunk right here is what the meal is.
[00:11:04]
I just unpack it right away as I iterate through the list, but if you prefer to do it
this other way, that’s just fine. Organize it in whatever way is intuitive for you and
that’ll work.
[00:11:20]
So this general pattern you’re going to use repeatedly. You’re going to use it on
your homework, you’re going to use it on the project. And so I encourage you to
walk through this code and understand how the different pieces fit together and
what they’re doing and how a tuple is being returned, treated, unpacked later on
so that you’re able to write these kinds of programs. And with that, have fun!