Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python 3 ( I will rate the answer) (Only Python 3 programmers only!) Edit your program from assignment 6 Write a new function that

Using Python 3 ( I will rate the answer) (Only Python 3 programmers only!)

Edit your program from assignment 6

Write a new function that returns a transposing dictionary:

It has two parameters: whether the original chords use sharps or flats, and the number of half-steps

Build the dictionary using each note as a key and the note offset by the number of half-steps as the associated value

For example, if the sharps were used, the notes are A ,A#, B, C, C#, etc. And if the number of half-steps is 10, the dictionary would be {A:G, A#:G#, B:A, C:A#, C#:B, etc}.

Edit your function that transposes:

Remove any code that produces a scale of notes (since this is now performed in the dictionary function)

Before the loop where you transpose, call your function that makes the transposing dictionary

Inside of the loop where you transpose, use the dictionary to determine the transposed root

After you have completed the previous steps, edit the main function:

Prompt the user for whether the result should contain sharps or flats

Pass this as a parameter when you call your transpose function

Edit both your transpose function and your dictionary function:

Add a third parameter for whether the result should contain sharps or flats

Edit the dictionary function

The dictionary keys will remain the same as previously.

However, the dictionary values will depend on whether the new parameter for the function indicates to use sharps or flats in the result. If sharp result, the dictionary values will include A, A#, B, C, C#, D, D#, E, F, F#, G, G#; if flat result, the dictionary values will include A, Bb, B, C, Db, D, Eb, E, F, Gb, G, Ab

Examples: Transposing "Eb D C F Bb G" -1 half-steps with a flat result should produce the string "D Db B E A Gb". But with a sharp result it should be "D C# B E A F#". Transposing "D# D C F A# G" +11 half-steps with a sharp result should produce the string "D C# B E A F#". But with a flat result it should be "D Db B E A Gb". Transposing "Eb F7 Bb7 Eb Gm Ab Gm Fm Eb" -1 half steps to sharps, the final result is "D E7 A7 D F#m G F#m Em D".

Down below is assignment 6. Now we need to edit it according to the instruction above.

def main(): # chords = "Eb F7 Bb7 Eb Gm Ab Gm Fm Eb" chords = input("Enter string of chords: ") # chords = "D E7 A7 D Gbm G Gbm Em D" halfsteps = int(input('how many half steps? ')) print(transpose(chords, halfsteps))

def makeChordList(s): return s.split()

def transpose(chrdstr, halfsteps): flat = "S Bb B C Sb S Eb E F Gb G Ab".split() # Flat notes sharp = "A A# B C C# D D# E F F# G G#".split() # Sharp notes if 'b' in chrdstr: scale1,scale2 = flat,sharp # keeping track of both flat and sharp scales else: scale1,scale2 = sharp,flat

chrdlst = makeChordList(chrdstr) upChords = [] # Tuple list tchords = [] # Turns it into a list

# creating tuple with root and variation split into components for c in chrdlst: x = [tuple([c.split(i)[0]+i, c.split(i)[1]]) for i in scale1 if i in c] # adding the delimiter after split if len(x) > 1: # removing faulty tuple from the data x = [x[0]] if len(x[0][0]) > len(x[1][0]) else [x[1]] upChords.append(x[0])

for c in upChords: # c is a tuple and c[0] is root to be transposed i = (scale1.index(c[0]) + halfsteps) % 12 if 'S' in scale1[i]: # modifying the scale from flat to sharp if note is 'S' to match the output tchords.append(scale2[i]+c[1]) else: tchords.append(scale1[i] + c[1])

return ' '.join(tchords)

if __name__ == '__main__': main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Development Of Knowledge Framework For Affective Content Analysis

Authors: Swarnangini Sinha

1st Edition

B0CQJ13WZ1, 979-8223977490

Students also viewed these Databases questions