Count dots on 5 black and white cards as one input (without a loop)

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the end user to enter 5 black and white cards representing bits, all as one line of input ('B' for black and 'W' for white), and displays the total number of dots as the output. You can do this without a loop, using 5 similar sets of blocks to process each of the 5 values.

Testing examples:

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

Input Output
WWBBB 24
BBBBB 0
WWWWW 31

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 5 cards ('B' for black and 'W' for white):] and wait

say (total number of dots)
if <(letter (1) of (cards)) = [W]> then
end

if <(letter (2) of (cards)) = [W]> then
end

if <(letter (3) of (cards)) = [W]> then
end

if <(letter (4) of (cards)) = [W]> then
end

if <(letter (5) of (cards)) = [W]> then
end
set [total number of dots v] to [0]

set [cards v] to (answer)

set [total number of dots v] to ((total number of dots) + (16))

set [total number of dots v] to ((total number of dots) + (8))

set [total number of dots v] to ((total number of dots) + (4))

set [total number of dots v] to ((total number of dots) + (2))

set [total number of dots v] to ((total number of dots) + (1))
Hints
  • Make a variable called “total number of dots” and set its value to 0. Make a string variable called “cards” and set its value to the input entered by the end user (5 black and white cards). Check each letter of the string and if it’s ‘W’ add the corresponding number of dots (16 for the first letter, 8 for the second letter and so on) to the “total number of dots”. Display the “total number of dots” 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

Show Scratch solution

Python
Block-based