Display the binary cards needed to represent a given number of dots

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user to enter a number of dots less than or equal to 31 as the input and displays which of the 5 cards should be showing as the output (one at a time).

Testing examples:

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

Input Output
11 8
2
1
1 1
8 8
31 16
8
4
2
1
32 Please choose a number less than or equal to 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 a number of dots less than or equal to 31:] and wait
if <<(number of dots) < [31]> or <(number of dots) = [31]>> then
else
end

if <<(number of dots) > [16]> or <(number of dots) = [16]>> then
end

if <<(number of dots) > [8]> or <(number of dots) = [8]>> then
end

if <<(number of dots) > [4]> or <(number of dots) = [4]>> then
end

if <<(number of dots) > [2]> or <(number of dots) = [2]>> then
end

if <<(number of dots) > [1]> or <(number of dots) = [1]>> then
end
set [number of dots v] to (answer)

set [number of dots v] to ((number of dots) - (16))

set [number of dots v] to ((number of dots) - (8))

set [number of dots v] to ((number of dots) - (4))

set [number of dots v] to ((number of dots) - (2))

set [number of dots v] to ((number of dots) - (1))
say [Please choose a number less than or equal to 31.]

say [16] for (1) secs

say [8] for (1) secs

say [4] for (1) secs

say [2] for (1) secs

say [1] for (1) secs
Hints
  • Make a variable called “number of dots” and set its value to the input entered by the end user. Below is the algorithm to help you program this:
If “number of dots” less than or equal to 31
  If “number of dots” greater than or equal to 16
    Subtract 16 from “number of dots” and display “16”
  If “number of dots” greater than or equal to 8
    Subtract 8 from “number of dots” and display “8”
  If “number of dots” greater than or equal to 4
    Subtract 4 from “number of dots” and display “4”
  If “number of dots” greater than or equal to 2
    Subtract 2 from “number of dots” and display “2”
  If “number of dots” greater than or equal to 1
    Subtract 1 from “number of dots” and display “1”
else
  Ask the end user to enter a number less than or equal to “31”
  • The if <> then block checks if the condition is true and then runs the blocks inside of the IF block. In this challenge you need to use IF statements to check if the “number of dots” is greater than or equal to 16 (use the OR block under “Operators”). If it is, display card “16” and subtract 16 from your number and do the same with the rest of the cards.
  • You can also use the IF THEN ELSE block (see image below) if you need to run a set of blocks if the condition is not true.
if <> then
else
end
  • Test your program with some values on the boundaries (i.e. 32, 31, 16, 8, 4, 2, 1)

Show Scratch solution

Python