Lists in Scratch 2

Scratch ListsVariables in Scratch allow us to store a single piece of data that we can use in our scripts. This blog post will introduce Lists (also known as arrays in other programming languages). Where variables allow us to store a single piece of information, lists allow us to store multiple pieces of information in the same way we would make a list of items on a sheet of paper. One of the most common lists we make in our lives is a shopping list, and I will use the idea of a shopping list to explain the concept of Scratch lists.

When we write a list on paper, we very often add items to the list from top to bottom, i.e. we add item 1, then item 2, etc. We can do this with lists in Scratch, and we can also insert items at a specific location in the list, remove items from a list, etc. In this post we will cover all the actions you can perform on a list. Code blocks will also be given to show exactly how you express this in the Scratch environment.

Creating a List

To create a list in Scratch, from the Block palette select Data and then click the Make a List button, give your list a name and then click OK, an empty list is then displayed on the Stage. At this point you are then ready to start adding items to your list. In all the example code blocks in this post, the list will be called ShoppingList.

Adding Items

To add an item to our list, we use the add block, which looks like:

Add Block
Where we see the word thing, we replace this with the word, number or sentence we want to add to the list. Looking at the picture of the list at the beginning of this post, we can see the first item in the list is Apples, so we replace thing with Apples, and we then have our first item in the list. If we then add another item, say Oranges, you will see that this is added to the bottom of the list. As we keep adding, the list grows. In programming terms, a list is dynamic in size, meaning it is not a fixed length.

As we are building a shopping list, we want to be able to add many items to it. The code block below allows this to happen. All the code is doing is repeating a block of code asking the user what they want to add to the list. If the user enters the word finish the code block stops.

Adding to a ListInserting Items

So, as we can see its very easy to add items to the end of a list. What about if we want to insert an item within our list? Well this is very easy to do by using the insert block. The insert block allows us to specify what we want to insert and the position in the list where we would like to insert it. If you click on the drop down on the insert block, you will see by default the options 1, last and random. Selecting 1 will insert the item at position 1 in the list, moving the current items in the list down by 1 position (so the current item 1 becomes item 2, etc), last will insert at the end of the list (the same as adding) and random will insert into a random position in the list. We are able to specify the exact position in the list we would like to insert our item. The code block below shows an example of inserting into a list.

List Insert
We simply specify what we would like to insert into the list and the position we would like to insert to. So, using the example of our list at the beginning of this post, if we inserted Milk at position 3, our list would then look like:

1. Apples
2. Oranges
3. Milk
4. Grapes
5. Bread
6. Cheese

As you can see, everything that was in position 3 onwards in the list has now moved down one position.

Deleting from a List

Deleting, or removing items from a list is achieved by using the delete block. You specify the item number you want to delete from the list. By default, the drop down in the block contains 1, last and all, but you can replace this with your required item number. the option all will delete every item from your list (essentially emptying the list), and this is something I would recommend that you do before at the beginning of your scripts. Generally you will always want your scripts to work with an empty list. The code block below shows how to delete an item from a list.

List DeleteReplacing items in a List

A nice feature provided in Scratch is the ability to replace an item in a list with something else. This is basically the same as performing the following two things:

1. Remove an item at a specific position
2. Insert an item at a specific position

The replace does this for us using the replace block. We simply provide the replace block with the location of the item in the list we wish to replace, and the item to replace it with. For example, again if we use our list given at the beginning of this post and we could replace Oranges at position 2 with Pears by simply using the block:

Replace Block
A slightly more complete script block could be as follows:

List ReplaceWhat else can we do with a List?

So, we have covered Inserting, Adding, deleting and Replacing items in a list, what else can we do? There are three more important things we can do with a list, these are:

1. Get an item at a specific position
2. Get the length of a list
3. Search a list for an item

Again, as with all the other list functions, there are blocks that allow us to do the three things. Lets have a look at these in order.

Getting an item at a specific position

Say we want to display an item at a specific position in the list, we use the item block. With this block, we specify the position of the item we want to return and this can then be stored in a variable or you can display it, etc. For our example we will display item number 3 (Grapes in our original list). To do this, we can use the following code block:

List ItemGetting the length of a list

As I’ve already mentioned, the size of a list is dynamic, meaning that the more you add, the bigger it becomes and there will come a time when you want to find out just how long your list is. You can find out the length of your list by using the length of block. In the same way you can return an item in a list to a variable, you can return the length of your list to a variable or again, display it. So, to display the length of our shopping list, we could use the following code block:

Length OfSearch a list for an item

Finally, we can search a list for an item. As you can imagine this is a very useful thing to be able to do. You can do this by using the contains block. The code block below, shows an very simple example of searching for an item in your list.

List ContainsConclusion

This has been a brief introduction to using lists in Scratch. I do hope you find it useful. Feel free to comment on this, use it in your teaching or any way you see fit.

I have produced a very simple Scratch project that was built to help write this post. The link for this is http://scratch.mit.edu/projects/11152013/

Advertisements

Nokia Lumia 520

Nokia Lumia 520 BlueI’ve had a mobile phone since the early 90’s when they were the size of a house brick. Over the years have replaced these with relatively useable phones. When work started to supply me with mobiles, I never really had a need to then replace my own phone. I’ve always had BlackBerry’s supplied to me and its never been that great an experience, but then, I don’t use Facebook or Twitter, etc on the phone.

Mid last year I decided to get myself a cheap “smart-phone” to play around with and to also enable me to switch the work phone off outside of work hours. This was a cheap Android phone which served my needs up until Christmas. For Christmas my wife bought me a Google Nexus 7 tablet which is a cracking bit of kit. I started to use this for all sorts of things, including Facebook and Twitter (before the tablet I used the good old PC!). I’m not a big Facebook user, so only give this a quick look maybe once a day, but I am quite a big user of Twitter and more recently Instagram. Whilst the tablet was great for this, it didn’t enable me to grab pictures or tweet anything on the go. The Android phone I had was slow and the camera, etc was not of great quality. Time for a new phone!!

My wife has a Samsung Galaxy S3 which is a brilliant phone, but I didn’t want to go for a contract that was going to cost the earth per month, so things like the S3, etc were out of the window. I also wanted to just be a little different and try something that the majority won’t, Windows Phone 8. Looking around I found the Nokia Lumia 520, which had some great reviews, so for a contract of £7.50 per month, I ordered one up.

Whats the Lumia 520 Like?

I have to say I’m impressed with this phone, I’ve been using it for a month now and have had no problems with it. Its light, responsive and feels like a quality phone. The screen size is 4 inches which is more than enough for my needs, picture quality is fine as well. I can’t comment on anything to do with music, as this is a side of the phone I will not be using. One gripe I do have is that the screen can get full of finger prints pretty quickly compared to say the S3 or even my cheap old Android, but a quick clean soon sorts this out.

The camera is also good. At 5MP the quality won’t blow you away, but its fine for capturing that priceless family moment. Battery life is ok, I wouldn’t mind a little bit longer, but then I do use it quite a bit. A charge over night sorts this out, and I certainly don’t want to be using the phone overnight!

In all, I’m pleased with the phone, it looks nice and feels like a quality phone.

Windows 8 Phone

So now we move on to Windows Phone 8, do I like it, is it any good? Well, the first thing to say is, it looks nice! That in itself isn’t a reason to use it, but I have to say, I do like it. Its a big departure from Android, but it has an intuitive interface and its easy to find your way around. As with everything Microsoft, you do need to have a Microsoft account (formerly a Live ID), I’ve had this for ages now, so upon switching on the phone, I entered my credentials and away I went. Well, not quite away I went. I generally have mobile data disabled on my phone and switch it on as and when I need it, so I did have to connect up to my router, but thats easy.

As I’ve already mentioned, I am a Facebook, Twitter and Instagram user, so my first port of call was to download the Twitter and Facebook apps. The Twitter app is not vastly different to the Android offering, so I felt at home. The Facebook app, whilst it works, isn’t particularly nice experience (nor is the Android one to be honest!), but it does its job.

Integration with SkyDrive is tight. Before having this phone, I was a SkyDrive user, so its nice that the integration is there (well you would expect it to be!!!). You can configure the camera app to automatically save pictures to your SkyDrive so there is no risk losing them from your phone. Obviously you will need a data connection to be able to do this (and it would soon eat into your allowance should you not be using WiFi), but its a very simple exercise to upload them to SkyDrive later on when you can use WiFi.

No Instagram?

Thats right, there isn’t an official Instagram app for Windows Phone 8, but there are some third party apps that will do the job. After a quick search around and a comparison between the offerings, I settled on the Instance app. There is a free and paid for version, I’ve gone for the free version, which shows a small advert at the bottom of the UI, which I can live with. It does everything I want and will fill the gap until an official Instagram app is released (maybe one day!!).

Managing Contacts

Managing contacts is a breeze using the People app. It allows you to integrate with Hotmail, Facebook, Twitter and LinkedIn, etc, and will import your contacts should you wish. I have imported my Facebook and Twitter contacts (as well as my contacts from my previous phone) and this is where I spend most of my time. Through the People app, I can look at all posts on Twitter and Facebook and allows you to tweet or post to a contacts Facebook wall, nice and easy. You could do away with Facebook and Twitter and just use People should you want to. I have removed the Facebook app and check all of this through People, but I have kept Twitter installed.

Live Tiles

Apart from looking nice, these are very useful. For example, from People, I have pinned my wife to my start screen, so I can pull up her details, call, text or contact her in any way available simply by using her tile. As the tile is live, it does also display any tweets, Facebook posts, etc she has last made, very useful. Integration to your email is very simple and again with a live tile, I get info on emails that have come in at a glance, very useful.

Was it worth it?

I have to say that I am very pleased with the phone and the operating system. Whilst I do like Windows Phone 8, it doesn’t do anything you can’t do with Android and I dare say the Apple offering (I’ve never used that so wouldn’t know). I am far from a power user of smartphones, and the phone and more importantly operating system does everything I need and I really do like the look and feel of it.

If you wanted to take the plunge and try something different like Windows Phone 8, I would recommend checking out the app store to ensure that everything is available that you currently use. For me pretty much everything is (come on Instagram!!!) from news apps to the social networking tools.

In all, very pleased with my purchase and its nice to be a little different sometimes!!

A new blog

Over the last few months I’ve been having problems with my hosting, where my site was not always available or was extremely slow. Having been paying for that hosting, I was less than happy with the situation and did seek a quick resolution, but this didn’t happen to my satisfaction.

As my site was originally a WordPress driven blog, it made sense to me to move over to WordPress themselves to get something a little more reliable, this is the result. I will be migrating my original content over the coming days, but in the mean time, keep checking for new posts, specifically on the Scratch 2.0 programming environment and Windows Phone 8.