Continued 7 Drag the hide va ar iable name block from the e Data category c and add it to the bottom of the code stack. Change n the value to sum if needed. This block tells Scratch not to display the sum variable on the stage while the program is running. u 8 Applying what you y have learned, add code to set the variable sum equal to the total of the value of firstNumber m plus the value of secondNumber.b 9 Applying what you y have learned, add code to ask k the user “What is the sum of these two numbers?” and wait for the user input. Remember, the user input is stored in the answer variable. 0 Applying what you y have learned, add the if then n else block to the code stack, and code the expres ss ion IF answer equals sum. This s condition tests if the user’s answer is correct. - In the first section o of the if then else block, add the h code to say to the user “That is correct!” This is the code that will be executed if the condi it ion is true. = In the second se ec tion of the if then else block, add d the code that will be executed if the condition is false, as shown. Join the statement “S So rry…the correct answer is with the value of the vari ia ble sum. when clicked set to firstNumber pick random to 10 1 set to secondNumber pick random to 10 1 hide variable sum ask What is the sum of these two numbers? and wait set to sum + firstNumber secondNumber else if then answer = sum say That is correct! say sum . join join Sorry...the correct answer is Goodheart-Willcox Publisher q Click the Green Flag button to run the program. Run it several times, and test for correct and incorrect answers. n w Save the project t as HOE05-03D in your working fo ol der. Try It! Create a project for testing user input. The user m mu st enter a whole number from 1 to 5. e If the input is within n this range, report to the user tha at the entry was valid. If the input is not correct, report t to the user “Error in entry. Not a number from 1 to 5.” What code will you use to check k if the entry is a whole number? ? Save the project as TI05-03D in your working folder. Green Flag Copyright Goodheart-Willcox Co., Inc. 118 Introduction to Computer Science: Coding
Previous Page Next Page