×

Challenges

Challenge 4.1

# Display MIDI note names

Challenge Level: Growing experience

## Requirement:

Write a program that asks the user to type in a number between 0 and 127 (60 is middle C) as the input and displays the corresponding MIDI note name as well as playing the note (the midi value mod 12 will be 0 to 11 for C, C#, D, Eb, E, F, F#, G, G#, A, Bb, B - and these can be looked up in a list).

Hints
• Make a list called notes and add the 12 notes: C, C#, D, Eb, E, F, F#, G, G#, A, Bb and B to your list (list of length 12).
• When you apply the mod 12 function to the midi value you'll get a number ranging from 0 to 11, which works well for Python since the index of a list starts at 0, and will go to 11 for a length 12 list.
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
60
C

Not yet run ?
34
Bb

Not yet run ?
85
C#

Not yet run ?
127
G

Not yet run ?
0

C

Not yet run ?