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

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()'.

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.

  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

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.

  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.”

Use join to merge the text “You guessed it! My secret number was” with the value of the secret number variable.