×

Challenges

Challenge 5.2

# Display the binary cards needed to represent a given number of dots (all in one line)

Challenge Level: Growing experience

## 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 all in one line.

Hints
• Make a variable called number_of_dots and set its value to the input entered by the end user.
• Make another variable called cards. 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 add 16,  to cards If number_of_dots greater than or equal to 8 Subtract 8 from number_of_dots and add 8,  to cards If number_of_dots greater than or equal to 4 Subtract 4 from number_of_dots and add 4,  to cards If number_of_dots greater than or equal to 2 Subtract 2 from number_of_dots and add 2,  to cards If number_of_dots greater than or equal to 1 Subtract 1 from number_of_dots and add 1,  to cards else Ask the end user to enter a number less than or equal to 31

• The if statement checks if the condition is true and then runs the lines inside of the if statement. In this challenge you need to use if statements to check if the number_of_dots is greater than or equal to 16 (use '>=') If it is, add '16, ' to the variable cards using an addition operator and subtract 16 from your number and do the same with the rest of the cards.

• You can also use an else statement to do something if the if statement is false

• In the extra challenge you need to ask the user to enter a number less than or equal to 31 if the input which was entered was greater than 31.

• Test your program with some values on the boundaries (i.e. 32, 31, 16, 8, 4, 2, 1)

Programming Reminders
# Print a string directly
print("Hello World!")

# Print a variable
print(my_var)

#### Variables

# Set a variable as a string
fruit_name = "Apple"

# Set a variable as an integer
pieces_of_fruit = 7

# Set a variable from a calculation
cost_of_fruit = pieces_of_fruit * cost_per_item

# Add one to a value
pieces_of_fruit += 1

#### Conditionals

# Find out the discount on fruit
if pieces_of_fruit > 100:
print("Bulk discount applies")
elif pieces_of_fruit > 5:
print("Discount applies")
else:
print("No discount")

#### For loops

# Print numbers 0-9 - remember Python starts counting from 0
for num in range(10):
print(num)

#### While loops

# Print numbers 0-9 using a while loop and a variable
num = 0
while num < 10:
print(num)

# Increment the variable by one.
# It will prevent an infinite loop!
num += 1 

#### Lists

# Create a list of fruit
fruit = ["Apple", "Banana", "Orange", "Pear"]

#### Functions

# Create a function which prints a greeting
def greeting(name):
print("Hello " + name)

# Call the function
greeting("Spiderman")

Enter your code in the editor below

Your results will be displayed here

Input Expected output Received output Status
15
8, 4, 2, 1,

Not yet run ?
1
1,

Not yet run ?
8
8,

Not yet run ?
31
16, 8, 4, 2, 1,

Not yet run ?
32
Please choose a number less than or equal to 31.

Not yet run ?