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
whenclickedaskPlease enter a number of dots less than or equal to 31:andwait
ifnumberofdots<31ornumberofdots=31thenelseifnumberofdots>16ornumberofdots=16thenifnumberofdots>8ornumberofdots=8thenifnumberofdots>4ornumberofdots=4thenifnumberofdots>2ornumberofdots=2thenifnumberofdots>1ornumberofdots=1then
setnumber of dotstoanswersetnumber of dotstonumberofdots-16setnumber of dotstonumberofdots-8setnumber of dotstonumberofdots-4setnumber of dotstonumberofdots-2setnumber of dotstonumberofdots-1
sayPlease choose a number less than or equal to 31.say16for1secssay8for1secssay4for1secssay2for1secssay1for1secs
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 ifthen 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.
ifthenelse
  • Test your program with some values on the boundaries (i.e. 32, 31, 16, 8, 4, 2, 1)

Show Scratch solution

Python