Checking Calculations The IF…THEN…ELSE statement can be used to compare the computer’s calculated answer with the user’s entry. An application of this technique may be to compare the user’s answer with the correct answer. In this case, the computer, in effect, is checking if the user is wrong. Error-checking is a g programming technique for verifying correct user input. This same technique can be used to provide a form of security. For example, the user input for a password can be compared to a list of valid passwords. If the correct password is entered, the user gains access to the software or web page. If an incorrect password is entered, a message appears. The message may be as simple as “Oops, you entered the wrong password,” or it can be more complex. In most cases, the user is given a few chances to enter the correct password. After three or four incorrect tries, the user could be locked out for some time period or forever. This can be achieved with a loop that repeats for a set number of times. Sometimes, a security question is used instead of a password. For example, “What is the name of your first pet?” is a common security question. Another common security question is, “What is the first name of your maternal grandmother?” All of these examples use IF…THEN…ELSE statements to compare what the user entered to what is correct or what data is on file. The comparison is either TRUE or FALSE. Hands-On Example 5.3D Hands-On Example 5.3D Compare Input p to a Calculation A simple program m can help the user improve basic ad dd ition skills. The program will display two random numbers and d ask the user to input the sum. The program will calculate the sum and compare that to the user input. I If the user’s answer is not correct, th he program will display the correct answer. 1 Launch Scratch. 2 Select a sprite an nd backdrop. 3 Applying what you y have learned, create three loc ca l variables: firstNumber, secondNumber, and sum. The firstNumber s and secondNumber vari ia bles will hold the randomly generated numbers. The sum u variable will hold the calculated t answer. 4 Applying what you y have learned, turn off the display p of the variable sum. Leave the other two variables vis si ble. The answer should not be s sh own to the user. Code will be used to display the answer w when appropriate. 5 Begin the code w wi th the when Green Flag clicked block. 6 Applying what you y have learned, add code to set the variables firstNumber and secondNumber equal e to random numbers betwee en 1 and 10, as shown. when clicked set to firstNumber pick random to 10 1 set to secondNumber pick random to 10 1 Goodheart-Willcox Publisher Continued Copyright Goodheart-Willcox Co., Inc. Chapter 5 Decisions 117