Before we begin this exercise, re-order your answers list so that all the positive responses are at the end. Edit the list items in your text editor and then re-import the list. Make a note of where the first positive response begins:
If you double-click on the variable monitor block on the stage, a slider will display. Use the slider to assign a number to the variable before you run the script as a way to test.
We asked our teller to issue only a positive response every fifth question, but we needed a way to let our teller sprite determine when the fifth question was asked. We set up the question_number variable as a way to count the question, and the seeker script updated the value of the question_number variable each time we pressed the space key. We call that a counter variable.
The mod block gave us the logic we needed to let the teller calculate whether or not to issue a positive response. The mod block divided the first number (question_number) by the second (5) and returned the remainder.
The teller sprite used the if block to compare the remainder to zero. We chose zero because when question_number is a multiple of 5, the remainder was zero. When the remainder was zero, we executed the code in the if block; otherwise, the code in the else block ran.
Let's evaluate some mod calculations using a divisor of 5:
In our script, question 25 guarantees a positive response, while questions 32 and 67 do not.
When we need to know how many items we've processed like we did in our "force a positive fortune" exercise, we use a counter variable. A counter variable is just an arbitrary name I chose so that we can easily associate that we are using a variable to count the steps in some process.
For example, iterating through each item in a list is a common example of using a variable to count the current list item's position. Consider the block of code in the following screenshot:
Imagine if we had used a list to store our jokes in Chapter 5. Our scripts would have been much simpler to construct. The sample code in the screenshot uses the counter variable in several ways. It sets the value to 1 prior to checking the condition in the forever if block. It uses the number assigned to counter to determine if we've processed all the items in the list. If counter is less than the number of items in the list, the block runs. At the end of the block, we increment counter's value by 1, and the forever if block checks the new value of counter.
If we identify an interval, then we can create a pattern of events based on the interval. We already saw an example where we look for the fifth occurrence of an event, but what if we wanted to make our sprite dance after 100 seconds elapse? A mod calculation helps us identify the interval. Assuming our timer starts at zero, the expression "current_time mod 100 is 0" becomes a check to identify every 100th second.
In our project example, we used the mod block to select certain items from our list, but we could program any number of events based on our interval, such as select items from a totally different list, change backgrounds or costumes, or we could use the mod calculation to do nothing at all.
Give mod a try. Make the seeker sprite do something on every fifth response. Examples of things you might try include issuing a response to the teller, applying a graphical transformation, or jumping for joy.
In earlier chapters, we became familiar with forever, forever if, and if concepts. Each of these concepts checks a condition and then runs if the condition is met. We don't define what happens when the condition is not met.
In contrast, the if/else control block evaluates a condition, and if the evaluation is true, the code in the if block executes. If the if condition evaluates to false, then the code in the else block executes.
Think of the ultimatums you give your children, or your parents gave you. If you clean your room, you get ice cream. Or else, you go to bed without a snack.