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.
repeat until
to ask the player () to guess the secret number until their equals the secret number.script variables
block to use it.ask
and answer
blocks go together. If you use ask
to ask a question, the user's answer will be reported by answer
.DISPLAY("Why did the chicken cross the road?") userResponse ← INPUT()
INPUT()accepts the value from the user and returns that input value, which is then assigned to the variable
userResponsewith 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.Repeat until
makes its decision based on the output of a hexagonal predicate block.
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.
mystery function
will report "finished"?
repeat until
loop will stop, and the function will report "finished."
report
command isn't inside the if
.
repeat until
block checks the value of input every time through the loop, not just its initial value.
repeat until
work?
repeat until
block runs the code in its script slot until input = 5, then the computer skips down to the next command, report (
finished)
, without the sprite ever saying 5.
repeat until
block tests the value of input before running the code in its script slot.
if
block do when input is 9?
set
instruction do to the value of input?
if
command run?
if
command will not run.
join
to merge the text "You guessed it! My secret number was" with the value of the secret number variable.