依赖于索引的代码无法正常工作

时间:2019-06-14 11:46:42

标签: python jython

尽管我没有收到代码错误消息,但并没有给我想要的数据。

我尝试在某些地方更改代码,但是很多更改都是​​表面上的,并没有给我带来更好的结果。我相信我的代码中有很多问题,而且我很难找到真正的罪魁祸首。

代码如下:

from music import *

from random import *

solo = Phrase()

solo.setTempo(90)

durations = []

pitches = []

x = 0

y = 0

z = 0

numberOfNotesInSolo = 0

listOfAvailablePitches = [48, 50, 52, 53, 54, 55, 56, 57, 59, 60, 62, 64, 65, 66, 67, 68, 69, 71, 72]

chordTonesList = [48, 53, 56, 60, 65, 68, 72]

startingPitch = choice(chordTonesList)

pitches.append(startingPitch)

y = pitches[-1]

nonChordTonesList = [50, 52, 54, 55, 57, 59, 62, 64, 66, 67, 69, 71]

weightedProbabilitiesForPitches = [listOfAvailablePitches[0]] * 20 + [listOfAvailablePitches[1]] * 20 + [listOfAvailablePitches[2]] * 20 + [listOfAvailablePitches[3]] * 20 + [listOfAvailablePitches[4]] * 20 + [listOfAvailablePitches[5]] * 20 + [listOfAvailablePitches[6]] * 20 + [listOfAvailablePitches[7]] * 20 + [listOfAvailablePitches[8]] * 20 + [listOfAvailablePitches[9]] * 20 + [listOfAvailablePitches[10]] * 20 + [listOfAvailablePitches[11]] * 20 + [listOfAvailablePitches[12]] * 20 + [listOfAvailablePitches[13]] * 20 + [listOfAvailablePitches[14]] * 20 + [listOfAvailablePitches[15]] * 20 + [listOfAvailablePitches[16]] * 20 + [listOfAvailablePitches[17]] * 20 + [listOfAvailablePitches[18]] * 20        

weightedProbabilitiesForDurations = [0.25] * 40 + [0.5] * 30 + [0.75] * 20 + [1.0] * 10 + [1.25] * 5 + [1.5] * 5 + [1.75] * 5 + [2.0] * 5 + [3.0] * 5

duration = choice(weightedProbabilitiesForDurations)

durations.append(duration)

while numberOfNotesInSolo < 60:

   while x == 0:  

      pitch = choice(weightedProbabilitiesForPitches)

      if pitch in chordTonesList and y in chordTonesList:                           

         pitches.append(pitch)

         x = 1

      elif pitch in nonChordTonesList and y in chordTonesList and ((listOfAvailablePitches.index(pitch)) - (listOfAvailablePitches.index(y)) == 1 or -1) or ((listOfAvailablePitches.index(y)) - (listOfAvailablePitches.index(z)) == 1 or -1):               
         print pitch
         print y
         pitches.append(pitch)

         x = 1

      elif pitch in nonChordTonesList and y in nonChordTonesList and ((listOfAvailablePitches.index(pitch)) - (listOfAvailablePitches.index(y)) == 1 or -1):                

         pitches.append(pitch)

         x = 1

      elif pitch in chordTonesList and y in nonChordTonesList:               

         pitches.append(pitch)

         x = 1

      else:

         x = 0  

   weightedProbabilitiesForDurations = [0.25] * 130 + [0.5] * 20 + [0.75] * 15 + [1.0] * 10 + [1.25] * 5 + [1.5] * 5 + [1.75] * 5 + [2.0] * 5 + [3.0] * 5

   duration = choice(weightedProbabilitiesForDurations)

   durations.append(duration)

   x = 0

   y = pitches[-1]

   z = pitches[-2]

   numberOfNotesInSolo = numberOfNotesInSolo + 1     

solo.addNoteList(pitches, durations)

print solo

Play.midi(solo)

Write.midi(solo, "solo.mid")

此声明尤其如此:

elif pitch in nonChordTonesList and y in chordTonesList and ((listOfAvailablePitches.index(pitch)) - (listOfAvailablePitches.index(y)) == 1 or -1) or ((listOfAvailablePitches.index(y)) - (listOfAvailablePitches.index(z)) == 1 or -1):

应该在listOfAvailablePitches上查找彼此为一个数字的索引(例如,索引2和3)。我收到的输出根本无法反映出这一点。

0 个答案:

没有答案