Page 2: Checking the Player’s Guess
Unit 2, Lab 1, Page 2
On this page, you’ll continue to develop your number guessing game to accept player guesses until the player guesses correctly.
You’ll want the computer to ask players to guess again and again until they guess correctly. To do that, you will use the block. Repeat until
is a loop (just like repeat
, forever
, and for
) but also a conditional (like if
and if else
). It repeats until a certain condition is met. For this program, the code should repeat until the player’s answer equals the secret number.
You learned about loops on Unit 1 Lab 3 Page 6: Looping with a Counter.
You’ve seen conditionals on Unit 1 Lab 2 Page 5: Adding Variety to Gossip
and Unit 1 Lab 5 Page 2: Sprite Following a Sprite.
- Use
repeat until
to ask the player () to guess the secret number until their equals the secret number.
- Drag the secret number variable out of the
script variables
block to use it. - The
ask
andanswer
blocks go together. If you useask
to ask a question, the user’s answer will be reported byanswer
.
The code would be written as
DISPLAY("Why did the chicken cross the road?") userResponse ← INPUT()
or .
Notice that the procedure INPUT()
accepts the value from the user and returns that input value, which is then assigned to the variable userResponse
with the ←
syntax. In Snap!, this is just like how answer
accepts a value from the user and reports it, and that report is what the computer set
s the variable user response
to.
- After the player guesses the secret number, make the computer congratulate the player.
- Test and debug. Take turns playing the game, and fix any problems with the code before moving on.
Repeat until
makes its decision based on the output of a hexagonal predicate block.
: Predicate and Boolean value
The word Boolean is capitalized because it’s named after a person, George Boole, who invented the branch of mathematics dealing with Boolean operations (such as and
, or
, and not
).
A predicate is a hexagon-shaped reporter that asks a true/false question such as these examples:
Predicates report a Boolean value (either or ).
Predicates fit into a hexagonal input slots of conditionals, such as in and . Predicates help conditionals decide when to do something.
The if
and if-else
blocks are called conditionals because they control the code based on a true-or-false condition.
- When the computer congratulates the player for guessing correctly, have the computer say the number. For example, it might say, “You guessed it! My secret number was 7.”
Use join
to merge the text “You guessed it! My secret number was” with the value of the secret number variable.