-2

I need to modify this code, to replace the number 1 with the number of the current measure. So, the first number in each measure will always rise.

instead of

1 
2 
3 
4 
1 
2 
3 
4 
1
2 
3 
4 

(with each number on its own line), I'd now print

1 2 3 4 2 2 3 4 3 2 3 4

, and so on.

beats_per_measure = 4
measures = 5

for measure in range(0, measures):     
    for beat in range(1, beats_per_measure + 1):
        print(beat)       
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Nado
  • 13
  • 3

3 Answers3

2

Perhaps you need something like below.

beats_per_measure = 4
measures = 5

### loop from 1 to 5 measures ###
# remove +1 to get sequence of 4
# iterations
for measure in range(1, measures+1):

  # print the measure value in a single line
  # first at iteration of outer loop to get
  # the sequence
  print(measure, end = " ")

  ## then loop from 2 to measure
  for beat in range(2, beats_per_measure + 1):
    # print each beat
    print(beat, end = " ")   

Output

1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4 5 2 3 4 
Anjan
  • 364
  • 1
  • 9
0

You can solve this by manually printing the measure you are currently in - followed by the remaining beats:

beats_per_measure = 4
measures = 5

for m in range(measures):  
    # manually print the measure you are in
    print(m+1, end=" ") # do not put a newline after the print statement

    # print the beats 0...bmp-1 == 0,1,2 - output adds 2 to each => 2,3,4
    for beat in range(beats_per_measure - 1):
        print(beat+2, end = " ") # do not put a newline after the print statement

Output:

1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4 5 2 3 4
*       *       *       *       *

The * are manually printed, the others filled in by the for-loop

You can read more about printing in one line here:

Doku: https://docs.python.org/3/library/functions.html#print


You can also create a generator that counts measures on its own (I'll mark the measure-number with * manually):

def gimme_measure(beats_per_measure): 
    beats = list(range(2,beats_per_measure+1))

    yield gimme_measure.measure 
    gimme_measure.measure += 1
    yield from beats 

gimme_measure.measure = 1  # defines the starting measure


# print 2*10 measures
for _ in range(10):
    print(*gimme_measure(4), end = " ") # the * decomposes the values from the generator
for _ in range(10): # continues the beat measuring
    print(*gimme_measure(4), end = " ") # the * decomposes the values from the generator

Output:

1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4 5 2 3 4 6 2 3 4 7 2 3 4 8 2 3 4 9 2 3 4 10 2 3 4 11 2 3 4 12 2 3 4 13 2 3 4 14 2 3 4 15 2 3 4 16 2 3 4 17 2 3 4 18 2 3 4 19 2 3 4 20 2 3 4 
*       *       *       *       *       *       *       *       *       **       **       **       **       **       **       **       **       **       **       **

The generator gimme_measure has it's own measure counter which is initialized to 1 and incremented each time you generate a new measure using the generator - if you do not reset the gimme_measure.measure to some other number it keeps counting upwards any time you print another generated measure.

You can even chain different bpm together:

# piece with 2 measures of 4 beats, 2 measures of 8 beats, 2 measures of 3 beats
for _ in range(2):
    print(*gimme_measure(4), end = " ")  

for _ in range(2): # continues the beat measuring
    print(*gimme_measure(8), end = " ")  

for _ in range(2): # continues the beat measuring
    print(*gimme_measure(3), end = " ") 

Output:

1 2 3 4 2 2 3 4 3 2 3 4 5 6 7 8 4 2 3 4 5 6 7 8 5 2 3 6 2 3
*       *       *               *               *     *   
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
0

If you are using python 3, this is the answer(tested):

beats_per_measure = 4
measures = 5

for measure in range(1, measures+1):
    print(measure, end = ' ')
    for beat in range(2, beats_per_measure + 1):
        print(beat, end = ' ')      

Or if you are using python 2, use this (tested):

beats_per_measure = 4
measures = 5

for measure in range(1, measures+1):
    print measure,  
    for beat in range(1, beats_per_measure + 1):
        print beat, 

More info here: https://www.quora.com/How-do-I-print-something-on-the-same-line-in-Python

hungnguyen
  • 64
  • 1
  • 10