Using Loops with Inputs

Last week, we covered what is a nested loop and how to implement it in SNAP! In the program we developed last week, we had minimal user activity. We defined the problem to draw 2 squares next to each other, and hard coded the number of squares as 2 in the program. There was no way for the user to define the number of squares he wanted the program to draw dynamically.

Today we will cover how to solve this problem and change our program to take user input on the number of squares the program should draw and then draw that many number of squares.

SNAP! blocks to ask and receive user input

To enable the program to prompt the user to give input and then use it in program, we will use 2 new blocks.

  1. The first block we will use is Ask and Wait Block in SNAP! block. This block is similar to the Say Block in SNAP! block but waits for a response from the user. The message we enter in the input box next to ask will be shown to user as a prompt, with an input box for user to enter his/her response.
  2. The second block we will use in this program will be Answer Block in SNAP! block. Once the user enters his response in the input box, described in 1., user’s input will be stored in this block.

Program to ask for user input and draw the number of squares equal to the user input

Now let’s update the program we built last week to ask for user input and use that to draw the squares. This is what it will look like:

draw the number of squares equal to the user input

Copy the same code in your SNAP! scripting areas and test it with different inputs.

Word of Caution

One thing to note while using Answer block in SNAP! block is that it can store only one value at a time. So if you are trying to capture multiple user inputs at the same time, you will need to store them in different variables.

Something like this will not work as intended:

Incorrect use of Answer Block


Fix the above code to get 2 inputs from user, one representing the number of squares and other representing the length of the square; and draw the squares with those dimensions.

Please feel free to share your questions/thoughts/feedback in the comments below.



Share This:

A geek, a techie, a daughter, a sister and a wife. Worked at Microsoft for 8 years and currently working at Amazon Web Services (AWS). All opinions expressed here are my own and has no correlation to my employer or ex-employers!

One thought on “Using Loops with Inputs

Leave a Reply

Your email address will not be published. Required fields are marked *