Count the black squares (one line of input)

Challenge Level: Growing experience

Learning outcomes

Students will be able to:


Create a program which asks the user to enter a series of black and white squares in one line of input (B for black and W for white) and returns the number of black squares as the output.

Testing examples:

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

Input Output
B 1



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

say (black cards total)

ask [Please enter a sequence of black and white squares (B for black and W for white):] and wait
change [index v] by (1)

change [black cards total v] by (1)
repeat (length of (cards))

if <(letter (index) of (cards)) = [B]> then
set [black cards total v] to [0]

set [index v] to [1]

set [cards v] to (answer)
  • Make a variable called “black cards total” and set its value to 0. Make a string variable called “cards” and set its value to the input entered by the end user. Check each letter of the string and if it’s ‘B’ add 1 to the “black cards total”. Display the “black cards total” as the output.

  • You can access a letter at the specified position in a string by using the letter (1) of [world] block under “Operators”. For example: letter (1) of [world] //w

  • In this challenge you need to access all the letters in user’s input and check to see if each of them is equal to B (black).

  • You can find how many letters a string has by using the length of [world] block unders “Operators”.

Show Scratch solution