SCRATCH GOLD II
SCRATCH GOLD I
2 of 3

4.03 A talking chatbot

Now that you have a chatbot with a personality, you’re going to program it to talk to you.

Click on your chatbot sprite, and add this code to it so that when it's clicked, it asks for your name and then says "What a lovely name!".

nano sprite

Click on your chatbot to test your code. When the chatbot ask for your name, type it into the box that appears at the bottom of the Stage, and then click on the blue mark, or press Enter.

Testing a ChatBot response
Testing a ChatBot response

Right now, your chatbot replies “What a lovely name!” every time you answer. You can make the chatbot’s reply more personal, so that the reply is different every time a different name is typed in.

Change the chatbot sprite’s code to join “Hi” with the answer to the “What’s your name?” question, so that the code looks like this:

nano sprite

By storing the answer in a variable, you can use it anywhere your project.

Create a new variable called name.

  • Click on Variables in the Code tab, then click on Make a Variable.Variable blocks
  • Type in the name of your variable. You can choose whether you would like your variable to be available to all sprites, or to only this sprite. Press OK
  • Create variable
  • Once you have created the variable, it will be displayed on the Stage, or you can untick the variable in the Scripts tab to hide it.Variable on the stage

Now, change your chatbot sprites’s code to set the name variable to answer:

nano sprite

Your code should work as before: your chatbot should say hi using the name you type in.

Testing a personalised reply

Test your program again. Notice that the answer you type in is stored in the name variable, and is also shown in the top left-hand corner of the Stage. To make it disappear from the Stage, go to the Variables blocks section and click on the box next to name so that it is not marked.

×
×

Cart