Count dots on 5 black and white cards as one input (using 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 input ('B' for black and 'W' for white), and displays the total number of dots as the output. Use a loop for this challenge, so it could be changed to work for a different number of cards.

Testing examples:

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

Input Output
BBBBW 1
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

repeat (5)
end

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

change [index v] by (1)

say (total number of dots)
set [total number of dots v] to [0]

set [index v] to [1]

set [number of dots v] to [16]

set [cards v] to (answer)

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

set [number of dots v] to ((number of dots) / (2))
Hints
  • Make variables called:

    • “total number of dots” and set its value to 0.
    • “number of dots” and set its value to 16.
    • “cards” and set its value to the string entered as the input. Check each letter of the input string “cards” (use a loop to iterate 5 times) starting from the first letter. 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.
    • “index” and set its value to 1. Use this variable to access a letter at the “index” position in the string.
  • 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 5 letters in user’s input and check to see if each of them is equal to B (black) or W (white).

Show Scratch solution

Python
Block-based