Check out a free preview of the full Get Kids into Coding with Scratch course

The "Using Lists" Lesson is part of the full, Get Kids into Coding with Scratch course featured in this preview video. Here's what you'd learn in this lesson:

Steve discusses creating and looping through lists in Scratch, which have a similar function to arrays.


Transcript from the "Using Lists" Lesson

>> let's look at one more thing, which is there. We saw variables before to keep track of the score. There's this other one over here. We'll talk about blocks in a little bit. There's this other one here called make a list. Right? And that is simply you know, for those, for those of you who have dabbled in the JavaScript programming language before An array, right?

And those are useful. There's no objects, so that'll bite us later, don't worry. We can make a list and again, this can be for this sprite only, or it can be for all sprites, right? I'm gonna say for this sprite only for now, and I'm doing this apparently on the cheese buffs.

Let's go for it. Why not? List name is catchphrases. Catchphrases, okay? So you can see I have these cheesy puffs catchphrases. There's nothing in there right now, right? There's a little plus sign. What are some of the things that cheese puffs say?
>> I'm cheesy.
>> All right I am Cheesy.

Exclamation point or period? Is a statement.
>> Exclamation [LAUGH].
>> Exclamation, yeah, it's a good choice.
>> But three of them.
>> Three exclamation points. Okay, what else do cheese puff say.
>> Hello.
>> Hello, I think with a period a very Hello. All right. Any other things that cheese puffs might say?

>> I am non sentient.
>> Okay, the like that they say that I am non sentient that's not suspicious at all. No, I am not. I am totally non sentient about that like just to make it very clear like in no way am I sent Dan. Do anything you want the team loves to say Now okay, well three sayings.

But the cheese puff say I will add one more. I am totally healthy food. As long as we got lion cheese puffs, they might lie about lots of things, right? Cool. So now what I want to have happen when I click on it, I want it to rotate through its various settings, right?

So at this point, we're gonna need both that idea of a modulo from before, just to get some extra practice. And then also this idea of having this list. So, when we click on Arnold, it will get the message to say I am puffy, but when we click on the cheese puffs, then in this case, we want it to rotate through one of his catchphrases.

So we'll say, we'll go to these events over here, I'll say when this sprite is clicked. Okay, now what do we do? How do we, if I look inside my variables, and I go to the block, I can add things to the list. I can delete one of the things and get the first thing off the list to start with that.

And so we'll say looks And we'll say.
>> The one that you're going to do you pick random one and the one put pick random between one to four.
>> Okay.
>> Operators,
>> So we got operators and we'll say that we want to pick random between one and four, right?

Problem is, that sometimes I'm gonna get the same one twice. All right, so there's a good good first step for us, right? But sometimes it's clicking more than once. And I'm getting the same thing. We cool if we can get it to rotate through. So the same basic idea, it's like we want to be able to kind of go through the list, right?

One of the things we might have to do here is keep track of where we are, right? Unfortunately, friends, there are no for loops. So we've got to basically build our own for loop. The old-fashioned way. So one thing we can do is we can say we have a variable here.

We say and we only want it for this sprite only the cheesy puffs care about what catchphrase they're on. What do I call this? We can call it current, current, current. [INAUDIBLE]
>> Okay, that doesn't mean anything. [LAUGH] That's a current. Current number that's on. We don't really have a green flag in this case, but we need some way to like set that to one by default.

So we'll add a green flag for our setup.And so we'll say let's just start at one, and we'll see why we won't need this in a little bit. Set current to one. And then what we can do is say, we'll go into these operators, and we'll do that modular again.

We'll say the current. No, it's current, module, and then we have four in this case. And then we'll bump the current each time. We'll start out with just seeing if it works for one. Say item current, catchphrases so let's see. Well, that's going on. We got, say one.

All right. Current was still set to zero because I never hit the green flag. Yay, and the apple is lonely, in this case. Cool, and so that's useful here. So current is now one. I am cheesy. I am cheesy. I am cheesy. So what we want to do is bump the number up every time.

He says the first thing the first time he's clicked. Then we want to say the second thing. Then we want to say the third thing. So on and so forth, right? So even if we just put the current in here for a moment we can kind of see that mostly work.

So we'll say current, and then what we want to do is change current by one after he said whatever it is. So if you look here, it's one. Now it's hello. Now it's I'm totally non-sentient. I'm a totally healthy food. It's currently five, so what happens next? It's outside.

So that's why we use that module so it keeps wrapping around, because it'll always be 0, 1, 2, 3, 0, 1, 2, 3, 4. Right? Well, never before because divided by 4 will be 0 again. We'll have no remainder, right? But you cut to the problem, you figure out the problem already.

So if we do that, we'll hit the green flag, set it back to one I am cheesy. Hello, I'm totally non-sentient. Nothing.
>> You can also do it if it's greater than, like, change current by one. After that, if it is greater than four, then it will change it to one again.

>> We could do that, right? So, what Caleb said is, basically, if at any point if it's less than one, bump it up to one, right? And I am pretty sure that work. Let's try it out. So we'll say if the number keeps going up. The other thing we can do is if it's always going to be 0, 1, 2 or 3 and we want it to be 1, 2, 3 or 4, what can we always do this number?

Just add 1, right? So this will either be 0, 1, 2, or 3. We want 1, 2, 3, 4. So we'll just take that number. We'll always add 1. So now I'm cheesy. Hello. I'm totally non sentient. I'm a healthy food. I am cheesy. Hello, I am totally non sentient.

I am totally a healthy food. I am cheesy. We rotate through. This works, and I think we got the point of both broadcasting from the previous thing as well as the idea of cycling through things. In fact, we can actually move these together, which is now whenever Arnold is clicked, instead of the sprite itself, Arnold will say, hello, and the cheese puffs will say one of its catch phrases, right?

So now. Hello, two seconds, hello. Hello, I'm totally non sentient. Hello, I am a healthy food, right? This will break again in one case that we just make this slightly better, which is if we add one more catchphrase. [SOUND] What's this other catchphrase be? Pizza, there we go.

So there's a break again. So the other thing we can also do just to make this a little bit better. Totally not necessary, is we just do it based on the length of the phrases. So it'll cycle through plus one and now we can add stuff we can remove stuff we don't have, what's that for previously is what in program we call a magic number.

It meant something to us. But now we just do it based on the length. So now I can rotate through, hello. I regret the two seconds on this, I am cheesy. Hello. I am totally non-sentient. I'm healthy food And now it cycles through our new one. And so we can add stuff, we can remove stuff, and to Kayla's point, we can now hide this list because we've got some of that in place.

So we can start to like snap all these different ideas together, and add different For an interaction. So you can create an entire like some people aren't making games in scratch. What they're doing is making an entire plays or stories that like play out or things that are actually scratch ones we hit play the green flag.

And it's like teaching you how to do something in scratch, right? By having Arnold say the different instructions and it goes through an entire story. So you begin to tie all of these things together and kind of create all sorts of different like interactions like in our time together today we're gonna rather than go all the way in so the weeds on one thing we'll try a few different things and like you then you can kind of go and explore with whichever thing is the most interesting to you.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now