Checking the Player's Guess

People are getting confused by the On the Exam box and are trying to build the code in there. --MF, 8/1/23

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 repeat until 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.

Pair Programming Swap
  1. Use repeat until to ask the player (ask () and wait) to guess the secret number until their answer equals the secret number.
    • Drag the secret number variable out of the script variables block to use it.
    • The ask and answer blocks go together. If you use ask to ask a question, the user's answer will be reported by answer.
The code ask (Why did the chicken cross the road?) and wait; set (user response) to (answer) would be written as
DISPLAY("Why did the chicken cross the road?")
userResponse ← INPUT()
or a white rounded rectangle containing two smaller white rounded rectangles: The first one contains first the word 'DISPLAY' in all caps and then a smaller white rectangle containing the quoted text 'Why did the chicken cross the road?'. The second one contains the text 'userResponse ← INPUT()'.
AAP-3.A.8, AAP-3.A.9
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 sets the variable user response to.
    AAP-2.B
  1. After the player guesses the secret number, make the computer congratulate the player.
  2. 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
Read More Why is Boolean capitalized?

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:
8 > 7 reporting true 6 > 7 reporting false

AAP-2.E.1

Predicates report a Boolean value (either true or false).

Predicates fit into a hexagonal input slots of conditionals, such as in if block and repeat until. 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.

Click for examples of predicates being used inside conditionals.
more complicated who {
    if (pick random (1) to (4)) = (3) {
        report (join (who) (', who') (does what) ( ) (who) (,))
    } else {
        report (who)
    }
} when green flag clicked:
repeat until (touching (Leader)?)
{
    point towards (Leader)
    move (1) steps
}
  1. We really shouldn't be encouraging students to mutate a block's inputs. There are really two issues here; one is with the x=x+1 style of programming altogether, and the second is that formal parameters specifically shouldn't generally be mutated; it's better to make a script variable that's initialized to the input value. (If we change this, we need to update the Guided Notes.) --MF, 6/26/21 with BH
    mystery function (input):
if (input < 5) (repeat until (input = 5)
(
    say (input) for (1) secs,
    set (input) to (input + 1)
)
report (finished) Which inputs to mystery function will report "finished"?
    Any integer
    Only integers greater than or equal to 5
    Only integers less than 5
    Only 5
  2. mystery function (input):
if (input < 5) (repeat until (input = 5)
(
    say (input) for (1) secs,
    set (input) to (input + 1)
)
report (finished) What will the sprite say if you run the same function with the input 1?
    speech bubble saying '4'
    speech bubble saying '5'
    four speech bubbles saying '1', '2', '3', and then '4'
    five speech bubbles saying '1', '2', '3', '4', and then '5'
    The feedback is not finished because I realized that BH might object to changing the value of an input variable... --MF, 3/1/19
  3. mystery function (input):
if (input < 5) (repeat until (input = 5)
(
    say (input) for (1) secs,
    set (input) to (input + 1)
)
report (finished) What will happen if you run the same function with the input 9?
    The sprite will say 9, and the block will report "finished."
    The sprite will say 9, 8, 7, 6, and the block will report "finished."
    The sprite will say 9, 10, 11, 12, 13, … and keep counting, and the block will never report "finished."
    The sprite will say nothing, and the block will report "finished."
    The feedback is not finished because I realized that BH might object to changing the value of an input variable... --MF, 3/1/19
  1. 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."
  2. Use join to merge the text "You guessed it! My secret number was" with the value of the secret number variable.