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.
- The first block we will use is block. This block is similar to the 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.
- The second block we will use in this program will be 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:
Copy the same code in your SNAP! scripting areas and test it with different inputs.
Word of Caution
One thing to note while using 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:
Challenge
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.
Intresting, will come back here again.