CS 105 - Understanding Our Algorithmic World

CS 105 - Exercise Eight

Goals

  • Learn how to get textual input from the user
  • Get some more practice with lists

Prerequisite

There is no starter code for this exercise, so go ahead and visit https://snap.berkeley.edu/snap/snap.html and make sure you are logged in.

Objective

This is going to be a pretty simple exercise. Our goal is to simulate a conversation with the sprite. It will ask us questions, wait for our response and then make a comment.

User interaction

We have not formally pulled out the say block and say for block yet, though a number of you have found them.

These, curiously, live in the Looks palette.

They do what they, well, "say". If you haven't tried one, pull it out and click on it.

If we are going to have a conversation, however, we need a way to contribute. For that, we have the ask block, in the Sensing palette.

Again, this largely does what you would expect. Pull it out and click on it. When yuo do, the sprite will ask you a question, and then provide a place for you to type your answer. The "wait" part means that the script won't proceed to the next block until you click the checkbox next to your answer or press the return key on your keyboard.

So, where does your typed response go? I'm glad you asked. You will find the text you typed in is stored in a variable: answer variable. Go ahead and click on answer variable. You will see that it reports back your last response.

A simple greeting

Create a short script to say hello to the user.

It should start by saying "hello" to the user. After a second or two, it should ask "What is your name?".

Then, the sprite should say "Nice to meet you name", where name is the name the user provided. In order to combine the text "Nice to meet you " and the response stored in answer variable, you should make use of the join block (which hopefully you will recall can be used to glue two pieces of text together to make one bigger one).

Click the script and introduce yourself.

Question time!

Now, it is time to make the sprite a little chatty.

Create a new variable called questions. It can be at the sprite level.

Use the set block to set questions to equal a list. Load the list with a couple of questions. They could be things like "What is your favorite color?", or "How are you doing today?". Be creative, write at least four of them.

To get the sprite to ask the questions, use a for block. Using ask block, length block and item block, ask each question, one after the other in the loop.

Click the script, and it should ask you a series of questions.

Answer time!

As conversations go, this seems a little one sided. Let's make the sprite even chattier.

Make a second list called responses. Set that variable to be a list of text as well.

We are going to be a little simplistic here, so what I would like you to put in this list is comments about your answer. So, for example, if the questions is "What is your favorite color?", the response could be "I like that color too!".

Write a response for each one of your questions. They don't make use of the user's responses, but it is more fun it it seems like they do. Each response should be at the same index in the list as the corresponding question.

So, for example, if the questions are ("What is your favorite color?", "Which instrument is your favorite?"), the responses could be ("That's mine too!", "Really? I am not that much of a fan, honestly").

Add a say for block so that after each question the sprite says the respective response. You don't have to use the user's answer (this is more challenging, but you certainly could figure out a way to do it if you want to).

Put it all together

Combine everything together so that when the green flag is pressed, the sprite greets you and learns your name, and then asks you the questions, making some remark after you answer each one.

Finally, I would like the session to end by the sprite saying "Well, it was nice chatting with you name", where name is the name of the user. This seems very straightforward, until you realize that answer variable now contains the response to the last question...

Create a new variable to store the name and save it right after you learn it form the user. Now, the final step should be pretty straightforward.

What I will be looking for

  • When I click the green flag, the sprite should say hello and ask my name
  • After asking my name, the sprite should ask a series of questions (at least 4)
  • Following each question, the sprite should make some comment about my answer
  • Finally, it should say "Well, it was nice chatting with you name", where name is the name I typed in at the start

Submitting

Share the project using the instructions from exercise 1.

Visit the exercise page on Canvas to submit the URL.