Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £35.99 £24.99. New subscribers only T&Cs apply

Find out more

Creating a Story

Watch this video to learn how to use variables to create a personalised story.
In this video, I’ll show you how to create the personalised story that is generated using data that’s been inputted by the user. I’m going to ask the use of two pieces of information for my story. I’m going to ask them to provide their name and also, their favourite food. So the first thing I’m going to do is I’m going to create two variables to store these pieces of information.
So I’m going to create a variable that’s called name.
And I’m going to create another variable that’s called food.
Now, we’re going to get the information from the user through the Ask block and that’s in the Sensing section, here.
So let’s drag and drop this in. And if I click to test it, you can see that the dinosaur sprite asks the question– it says, what is your name? And a text box appears at the bottom for me to type my answer in. So I’m just going to type Bob, to test this out. And then you can press Return or click this tick box here. Now what’s happened is that the answer to this question has been stored in a variable over here, called answer, which is a built-in variable in Scratch. And if I click the tick to show the contents of this variable, you can see that the answer here has been sent to Bob– which is what I entered.
And if I want to store the data in this variable somewhere else, all I need to do now is to set my name variable to be the answer that was provided. So I’m going to use a set block for this. And I’m going to say that I want the name variable to be set to the value of the answer. And I can drag and drop that variable, and it fits nicely into that space there.
So now, I’ll test it with a different name. I’m going to use Kim this time. And you can see that what’s happened is that the answer variable is holding that data that I’ve entered. And so is the name variable that I’ve programmed, too. Now I’m going to do the same with the next question. So I’m going to duplicate this code.
And then the next question I’m going to want to find out is the user’s favourite food. So I’ll alter this text here.
And this time, I want the food variable to be set to the user’s answer.
And I’m going to do all of this where the green flag is clicked. So let’s test it out. And you can see that, first of all, I’m asked for my name. So I’m going to type my real name in this time. And then it asks me for my favourite food. So I’m going to use my real favourite food– cheese. And now you can see that the name variable is holding my name and the food variable is holding my favourite food.
So I now have all the data that I need for my story and the next thing is to write the story itself. And to do that, I’m going to use a say block. And the first thing I want to say is here is your story.
And then, for the first line of my story. I want to say there once lived a creature called and then add a name, which is the name that the user has entered. So there’s two parts to this sentence. First, it’s a phrase that once lived a creature called. And secondly, there’s the creature’s name. So to do this, I’m going to use a join block.
And what I want to say is the initial phrase and then I want to join this onto the name that the user has entered. So I’m going to use that variable as the second part of the sentence.
Now if I click these blocks to test them out, you’ll see that we get the story. And we get the first line. Except you can see that there was no space between the word called and my name. So I’m just going to detach this and just test it out, again. So I can change that by adding a space after the word called. And now if I clicked this block to test it, again, you’ll see that the sentence does have all the spacing in correctly now so I’ll pop it back into the same block. Now, let’s test it once more– that looks a lot better.
And then, for the next line of my story, I want to tell the user what the character’s favourite food is. So again, I’m going to use a say block. And this time, I’m going to join another two phrases.
And the first part of this phrase is going to be who loved.
And then, I’m going to click on the Variables as select Food.
And put it in the second part of the join block. So that will tell the user what the character’s favourite food is. Let’s have a look.
And you can see that, again, I forgot to put a space in the join block after the word loved. So I’ll add that in and I’ll test it, again.
That looks great. So now, I’m going to join the two sections of the script together. And I am going to test everything all at once now. So this time, I’m going to type the name as if.
And the favourite food is going to be bananas. And you can see that this time, we get a slightly different story. Now, these variables at the top here on the stage are really handy to see when you’re testing the project, but we don’t need to see them in the finished project. So I’m going to hide these by unticking the boxes. And I’m also going to hide the answer variable, too. So there we have it– a very short, but interesting story.

In this video you’ll learn how to create the personalised story seen in the previous example, by using variables to store data input by the user.

This article is from the free online

Teaching Programming in Primary Schools

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now