Question
Problem 2 - Minor Key Here is a list of musical notes, written with flats (rather than sharps): CDb, D, Eb, E, F, Gb,G,Ab. ABb. B, C in the next octave) Observe that not all notes have flats, Le.Cflat is actually B which is not a black key on the piano for instance The harmonic minor scale is composed of the following sequence of steps: Step] [ Step) | Step) [Step] [13 Step] [1% Step] [ Step] For instance, the Charmonic minor scale is C, D, E, F, G, A.B.C. Each musical "half step" is a single step through the list, the name half-step predates computers so you have to forgive musicians a little. The Eb harmonic minor scale is Eb F Gb Ab Bb BDE The G harmonic minor scale is GABb CDE5 GbG Define a list: MUSICAL NOTES - ['c', D\u2660', 'D', 'E\0266d', TE", "Gu2663', 'G', A\u266d', 'A', '\u2660". Note that \u266d' is the unicode for the flat symbol Allow the user to enter starting notes like: "E* or "B flat" or "flat" You will have to calculate the starting note from this, or if it's not a note, for instance if they enter "R" Then display the harmonic minor scale starting at that note, or an error message indicating that it is not a note. When the user enters "quit then stop asking for notes and exit the program,
My suggestion is that you break this problem down into parts: 1) Translate the input into the musical notation by creating a Thelper function' which takes as a parameter the user's input and outputs the proper note, meaning it will replace 'flat' with b and remove the space, so that it matches the description in the list of strings. 2) Compute the starting index in the list of musical notes or determine that the starting index doesn't exist because the note isn't a real note. I use another helper function for this purpose. 3) Calculate the scale itself in a slightly bigger function but then return it as a list. 4) Outside of your function, implement a main loop where you will join the answer together and output it. This is generally how programmers think about problems. We attempt to break each problem into subproblems which we can write small functions to solve. Then we put those functions together and get our final results.
Sample Output linux5[109] python3 minor_key py Enter a starting note (D. E flat): G GAB CD EGG Enter a starting note (C, D flat): A ABCDETA A Enter a starting note (c, Dalat): A flat A B B D E EGA Enter a starting note (C, D flat): flat There is no starting note R. Enter a starting note (C, D flat): quit linux5[110]\ python3 minor_key.PY Enter a starting note (D, E flat): C CDEFGABC Enter a starting note (C, D flat): D DEFGAB DD Enter a starting note (C, D flat): E flat EFG A B BDE Enter a starting note c, flat): G GAB CD EGG Enter a starting note (C, D flat): flat DEEA ACD Enter at ting note (C, D flat): quit
Asked By CrimsonShadow33 at
Answered By Expert
Stephen
Expert Ā· 5.4k answers Ā· 5k people helped
musical_notes = (
"C",
"D\u266d",
"D",
"E\u266d",
"E",
"F",
"G\u266d",
"G",
"A\u266d",
"A",
"B\u266d",
"B",
)
valid_inputs = [x.replace("\u266d", " flat").lower() for x in musical_notes]
def harmonic_minor_scale_generator(start_index):
scale_indices = [0, 2, 3, 5, 7, 8, 11, 12]
scale_list = []
for x in scale_indices:
scale_list.append(musical_notes[(x + start_index) % 12])
return scale_list
playing = True
while playing:
in_data = input("Enter a starting note (C,D flat):").strip()
if in_data.lower() == "quit":
playing = False
elif in_data.lower() in valid_inputs:
start_index = valid_inputs.index(in_data.lower())
print(*harmonic_minor_scale_generator(start_index))
else:
print("There is no starting note", in_data.replace(" flat", "\u266d"))
The following python code can be used to solve the problem mentioned above.
š§āš« More Questions
š Interested in exploring further?
Chrome Extension
1. Search answers from our 90+ million questions database.
2. Get instantly AI Solutions powered by most advanced models like GPT-4, Bard, Math GPT, etc.
3. Enjoy one-stop access to millions of textbook solutions.
4. Chat with 50+ AI study mates to get personalized course studies.
5. Ask your questions simply with texts or screenshots everywhere.