I'm currently trying to make a program that takes sheet music for violin and translates the given notes into a position on a string, but my problem is that when I ask if a key is sharp or flat, and how many sharps or flats are in that key signature I find that I'm making a bunch of tedious if/then statements such as:
if keysig == sharp and signum == 2:
note['LE'] == 'D4'
note['SC'] == 'A4'
elif keysig == sharp and signum == 3:
note['LE'] == 'D5'
note['SC'] == 'G2'
you'll see that it becomes super annoying to go back and make another statement with slight changes. How can I fix this?
For those of you that don't read sheet music: Basically what I'm trying to say is that this algorithm will take notes that the user inputs such as A, G, or D and turn them into exact locations on the violin finger board so you won't have to search for the notes manually. This program is intended for violin beginners that lack experience in reading sheet music.
You could use a dictionary:
transpositions = {
(sharp, 2): {'LE': 'D4', 'SC': 'A4'},
(sharp, 3): {'LE': 'D5', 'SC': 'G2'},
# etc.
}
note.update(transpositions.get((keysig, signum), {}))
This uses a tuple of (keysig, signum)
as the key, mapping to specific note transpositions. If no such signature is found, no updates are made (updating with an empty dictionary).