下面是我在作业4中的代码,它正在进行简单的和弦转换。现在,我们需要根据以下说明进行修改。
从作业4修改你的程序。目前你的和弦组成 只有根。但是,根据root可能会有额外的 字符。例如,我们可以有一个D和弦或D7,Dm,Dm7, Dsus2,Dmaj7等等。根后的字符(在此 案例D)描述和弦的不同变化。
编辑你的函数,使和弦字符串成为一个列表 和弦:在你创建(字符串)列表后,制作一个元组列表。 每个元组将由两部分组成:和弦的根 和弦的变化。例如," D"成为(" D",""), " DSUS2"成为(" D"," sus2")," F#"成为(" F#","")," F#m"变 (" F#"," m")请注意,根是一个或两个字符。该 和弦的变化是零个或多个字符。
编辑转置的函数:迭代列表时 和弦元组,只转置和弦的根。剩下的 和弦(它的变化)不受影响。例如,如果转置+3 半步,(" F#"," m")会产生和弦" Am"和(" C","") 会产生和弦" D#"。
如果主函数中的原始字符串是" Eb F7 Bb7 Eb Gm Ab Gm Fm Eb"我们转换-1半步,最终结果是#E; D E7 A7 D. Gbm G Gbm Em D"
这是我的作业4。我不知道下一步该做什么。
def main():
chords = 'Eb D C F A# G'
halfsteps = int(input('how many half steps? ')
print(transpose(chords, halfsteps))
def makeChordList(s):
return s.split()
def transpose(chrdstr, halfsteps):
if 'b' in chrdstr:
scale = "S Bb B C Sb S Eb E F Gb G Ab".split()
else:
scale = "A A# B C C# D D# E F F# G G#".split()
chrdlst = makeChordList(chrdstr)
tchords = [] # Turns it into a list
for c in chrdlst:
i = (scale.index(c) = halfsteps) % 12 # find "c" from scale. Using divide by 12 will bring it back to since
#
tchords.append(scale[i])
return ' '.join(tchords)
if __name__ == '__main__':
main()
答案 0 :(得分:0)
将和弦列表转换为元组列表是一个相当混乱的函数:
def makeTuples(listIn):
rootList = []
variationList = []
for i in range(len(listIn)):
tempLen = len(listIn[i])
if tempLen == 1:
rootList.append(listIn[i])
variationList.append("")
else:
if listIn[i][1] == "#" or listIn[i][1] == "b":
rootList.append(listIn[i][0:2])
if tempLen > 2:
variationList.append(listIn[i][2:])
else:
variationList.append("")
else:
rootList.append(listIn[i][0])
variationList.append(listIn[i][1:])
return list(zip(rootList,variation))
关于转置:
def transpose(tupleList, halfSteps, sharps):
returnList = []
if sharps:
scale = "A A# B C C# D D# E F F# G G#".split()
else:
scale = "A Bb B C Db D Eb E F Gb G Ab".split()
for i in range(len(tupleList)):
thisRoot = tupleList[i][0]
firstPos = scale.index(thisRoot) # Consider adding some error-handling here
newPos = (firstPos + halfSteps) % 12
returnList.append((scale[newPos], tupleList[i][1]))
return returnList
已实施,您的main()
可能如下所示:
def main():
chords = input("Enter chords, separated by spaces: ")
halfSteps = int(input("Enter number of steps: "))
myChords = getList(chords)
myTuples = makeTuples(myChords)
result = transpose(myTuples, halfSteps, True) # True for Sharps, False for Flats