2. Other Projects‎ > ‎Scratch‎ > ‎

Can I Tell You A Joke?

posted 2 Oct 2015, 12:41 by Andy Lakin-Hall   [ updated 2 Oct 2015, 12:44 ]
One of the features of Scratch which I use very little is the Lists function.

Hidden away under the orange   section, the   function allows you you create a list of words. There are functions which allow you to select any item of the list, and also to check to see if a particular item is in the list.

To showcase this function, I have created an example program which you can try building for yourself.

Getting Started

You can use the default Scratch Cat, but I've switched to Gobo. On the Gobo sprite, add the following blocks 

If you Green Flag this script, Gobo will ask you to enter your name, and then say "Hello," to you, and ask the important question; "Can I tell you a joke?"

The Dilemma

The problem with this simple question is, there are a lot of ways of saying "Yes" that the user might enter. Here are some possibilities;
  • yes
  • Yes
  • YES
  • ok
  • OK
  • okay
  • Okay
You need to think about ALL the ways that a user might ener "Yes" because if you miss one, your user will get frustrated.

So you need to build your first list. Go to  and then click  for this sprite only. Call the list "ways to say yes" and add all of the alternative ways you can think of to that list.

Now add the IF - ELSE Block like this;

Notice how in the IF part of this block, I have used the operator that checks to see if a particular item is is the list. Here I've selected the Ways to say yes list, and also used the variable answer, which contains whatever the user entered. This checks through the entire list to see if it matches. If it does then the broadcast QUESTION is launched (We will build that later).

If the list does not contain whatever the user entered, then the ELSE section runs, and you need something to happen as a response to that. I've made Gobo do a dance, and included a broadcast to make that happen.

The Dance

So here's the simple little dance. You could change this, or do anything else you like.

The Joke

Assuming the user enters "Yes" to the question, the next thing is to actually deliver the joke.

You need to create two new lists for this sprite only; one of joke questions, and one of joke answers.

 #  Joke  Answer
 1  What's brown and sticky?  A stick!
 2  Why to bees hum?  Because they don't know the words!
 3 How many legs has a horse got?  SIX! Fore legs in front and two behind!            
 4  What's yellow and dangerous?  A zombie banana!

Enter these  jokes - or your own better ones, into the two lists. Make sure the answer matches the joke, or things will look really weird!

It doesn't matter how many you include, but four would be the minimum.

Next you need to create a variable for this sprite, called Which Joke. This variable will store the result of a random pick.

Build up the following block;

See how the variable Which Joke is set to be a random number between 1 and the length of the list of jokes. That means that you could add more jokes to the list, and the program block will still work.

Now you've set Which Joke to say use the Say for 2 Seconds block to first say the joke, and then say the answer.

The script will end then, but if you want, you can put the whole thing in a Forever loop, so it goes back to the start and does it all again.

Here's the entire script;

You can download a copy of my example here;

But I'm hoping you'll build your own version.