Fruit 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 go through the “You already said that…” activity. So what’s
the premise? We need to write a program that queries a user for a list of fruits. If
the user says a fruit that has already been given, you say you already said that.
The program should ignore casing, uppercase versus lowercase. And once 10
fruits have been given, print way to go and end the program. And so that’s fruits
dot py.
[00:00:25]
So you see an example here, right? “Pear, apple, pear.” “You already said that.”
“Kiwi, banana, pear.” “You already said that.” “Apple.” “You already said that.”
And so things to think through. We know how to write a program that queries a
user for a list of fruits, for a list of anything, right? So there’s like some while true
with some input and we’re going to append those inputs to a list. All right. That’s
not too hard.
[00:00:49]
In fact, maybe before we go much further, we could start with that shell. So let’s
set up a loop where we’re going to add in all these fruits until we have 10 fruits,
then we’ll deal with this middle feature next.
[00:01:08]
So come over here, here’s our file. Fruits.py. Empty file, what do you just start
with? Def main pass. If name is equal to main, call main. Great. So now in here,
we’ve got our loop, while true, and we got our fruits. Fruit equals input fruit.
Fruits dot append fruit. If length of fruits is equal to 10, break.
[00:02:01]
And I guess then what do we do at the end? So we’re supposed to print it out.
Way to go, right? Print way to go. Great. Now we’re done.
[00:02:14]
Now we could simplify this a little bit, right? So instead of saying while true and
then having this down here, in this particular situation, it would be easy enough
just to say, while length of fruits is less than 10, append and append. Cool. Let’s
run that.
[00:02:35]
Run fruit, fruit, apple, apple, whatever. I have a pear, banana. A, a, a, a, a, a, way
to go. So we can get our list of 10. And then once we have all 10 in there, finish
this. Good.
[00:02:52]
That wasn’t the whole program, right? What are we supposed to do? If the user
says the fruit has already been given, you say, “You already said that.”
[00:03:03]
So what do we want to do? We have this fruit and now we need to decide, right?
If already seen, how do we know that we’ve already seen this? In this case, well,
we know everything that we’ve already had, and so if the fruit you gave me is
one of those, then I’m not supposed to accept it, right? How do we do that in
Python? We use the in operator. So if the fruit you gave me is in the fruits
already, print, “You already said that.”
[00:03:40]
Otherwise we’ll take it, right? And we’ll go from there. So now we can come
down here. Fruit: apple. Fruit: apple. You already said that. Pear, right? Pear, you
already said that. Apple, you already said that. Great. Rather than try and think
of a whole bunch of fruits, get some letters. Great. There it is.
[00:04:04]
I should change that 10 to something shorter for this demonstration. Maybe we’ll
go about three, make it quick. So there’s one other feature then that we wanted
to keep track of, right? Right now the way this is set up, run fruits, if I give you an
apple and then later an apple and an apple, it’ll take it, and we want to make it
so that no matter what casing I provide, it excludes it.
[00:04:39]
And the pattern typically for that is to standardize the case. So we make
everything uppercase or everything lowercase, usually lowercase. So I can say
you give me a fruit and then what I will do is I will just make it lowercase as it
comes in. So now the input, whatever you gave me, I turned it into lowercase
and then I’ll check those. And now it doesn’t matter which one you say, it’ll
always print it that way.
[00:05:07]
So let’s come over here. Fruit: apple. Apple. You already said that. Banana,
BANANA, APPLE, pear. Cool. So the idea is it’s a very simple little program, but a
few new features here to look at. One, we’ve changed the wild loop conditio.
Instead of while true, we just said, while the length of fruits is less than three.
[00:05:38]
Then as it grows, eventually it’ll stop. This is more similar to how we did it with
bit. You can use while true with the if and a break, that’s fine. But here’s another
way you can think about it.
[00:05:52]
And then we talked about this idea of standardizing the case. You know, we
didn’t print the fruits out. At the end, if we were to say way to go and then print
out the list of fruit that was provided, you’d notice that they’re all lowercase,
regardless of what we put in.
[00:06:08]
And maybe that’s fine, right? If you’re building this program for somebody else,
you have to go talk to them and say, hey, is it ok if I print them all out in lower
case or do they need to be preserved exactly as they are? And that would add
some complexity to the program.
[00:06:22]
But then also here in this, we’re using the in operator for fruit and fruits as a way
of checking to see if we already have a certain value. If it’s in the collection I
already have, I do one thing. Otherwise, I add it to my collection. And there you
go. That’s, “You already said that.”