×

Challenges

Challenge 8.1

# Binary representation of a decimal number between 0 and 31 by playing musical notes

## Requirement:

Write a program that asks the user to enter a decimal number between 0 and 31 as the input and plays the 5 musical notes representing that number in binary (a high note for dots showing and a low note for dots not showing).

Hints
• Make variables called:

• decimal_number and set its value to the input number given by the end user.
• bit_value and set its value to 32.
• Now divide the bit_value by 2 and check if decimal_number is greater than or equal to bit_value. If it is, print 'high' for a high note and subtract bit_value from decimal_number. If not, print 'low' for a low note Repeat while bit_value is greater than 1.

• Test your program with some values on the boundaries (for example test it with numbers 0 and 31).

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
11
low
high
low
high
high

Not yet run ?
31
high
high
high
high
high

Not yet run ?
0

low
low
low
low
low

Not yet run ?