Count the black squares (one colour per input)

Challenge Level: Beginner

Learning outcomes

Students will be able to:

Requirement:

Create a program which asks the user to enter 5 black or white squares (B for black or W for white) one at a time and outputs the total number of black squares.

Testing examples:

Your program should display the outputs shown in this table for the given inputs provided:

Input Output
B
W
W
B
W
2
B
B
B
B
B
5
W
W
W
W
W
0

Languages

Scratch

What it should look like

Click on the green flag, enter the inputs provided in the “testing examples” to see the expected output of your program.

Recommended blocks
when green flag clicked

ask [Please enter a black or white square (B for black or W for white):] and wait

say (black cards total)
set [black cards total v] to [0]

change [black cards total v] by (1)
repeat (5)
end

if <(answer) = [B]> then
end
Hints
  • To store the total number of black cards, create a variable called “black cards total”. You need to increase the value of this variable every time the user enters a black card. Display the value of “black cards total” at the end.
  • Use the repeat () block to run the blocks inside a specified number of times. In this challenge you need to repeat the blocks 5 times.
  • The if <> then block checks if the condition is true and then runs the blocks inside of the IF block.

Show Scratch solution