Cooperative Coding Using efficient nested IF…THEN…ELSE statements, computers can be programmed to make decisions. For example, a computer program can look at a person’s library activity. The system can send a statement, overdue notice, or warning without the librarian’s input. The program checks how many days from the current day it is until or past the due date. It uses this number to choose and send the correct message to the borrower. The same type of decision-making can be applied in banking. A program can look at a client’s account balance. Depending on the balance, the bank can send a simple statement or a low-balance warning. When a check is received, the program can compare the check amount to the account balance. If there is not enough money to cover the check, a notice of insufficient funds can be sent to the client. Work with a partner to create a Scratch project to play the game Rock-Paper-Scissors. This game requires the computer to make several decisions based on user input. Apply the algorithm developed in Chapter 2. The table shows the algorithm for determining the winner as devised in Chapter 2. Testing all of these outcomes requires nested if then else blocks. Player 1 Player 1 Player 2 Player 2 Outcome Outcome Result Result Second player throws rock. A tie. Play again. You throw rock. Second player throws paper. You lose. Game over. Second player throws scissors. You win. Game over. You throw paper. Second player throws paper. A tie. Play again. Second player throws rock. You win. Game over. Second player throws scissors You lose. Game over. Second player throws scissors. A tie. Play again. You throw scissors. Second player throws rock. You lose. Game over. Second player throws paper. You win. Game over. One player will be the computer. As the human player, you will choose one of the three items. The computer will make a similar choice based on a random number. Devise a way to make the computer’s selection based on a random number. Also, ask the user to input either “rock,” “paper,” or “scissors.” Be sure to include explanations so the player will know what to enter and how. Devise a way to check for correct user entry. Use nested decisions in this check. Hint: if the user has entered an incorrect choice, the stop all block can be used to end the program. Save the project as CoopCode05 in your working folder. 1. How did you use a random number to make the computer’s selection? 2. Briefly describe the coding you used to check for correct user input. Do you see any issues with the code you used? Programming Nested Decisions wavebreakmedia/Shutterstock.com Copyright Goodheart-Willcox Co., Inc. Chapter Ch Ch C Ch C C C C C C p p p p p p p p p p p p p te te te te te te te e te r r r r r r r 5 5 5 5 5 5 Decisions D D D D D D D D D D ec ec ec ec ec e e ci i 119
Previous Page Next Page