I'm looking for help to solve the below problem: Having 2 variables beat
and measures
I need to create a loop that prints, on the same line, all the beats times the number of measures. Nevertheless, every time it starts a new measure
, the first beat
should be equal to the number of the current measure
.
Example: beats_per_measure = 4
, measures = 4
should give
1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4
and not
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
beat = beats_per_measure 1
measure = measures 1
for i in range(1,measure):
for j in range(1, beat):
print(j, end = " ")
This is the code I've tried to print beats x measure, but I'm not sure how to change that first beat to reflect the current measure.
CodePudding user response:
This code gave:123123123, maybe you lost "space" somewhere.
for i in range(1,4):
for j in range(1, 4):
print(j, end = "")
CodePudding user response:
for i in range (1, measure 1):
for j in range(1, beat 1):
if j==1:
print(i, end="")
else:
print(j, end="")
CodePudding user response:
Don't overthink a problem like this; adding an extra print
provides the expected output, no if
required:
measures = 4
beats = 4
for measure in range(1, measures 1):
print(measure, end="")
# Start beat from 2
for beat in range(2, beats 1):
print(beat, end="")
CodePudding user response:
You could try to use the str.join
method:
beats_per_measure = 4
measures = 4
beats = beats_per_measure 1
print(" ".join(
str(n) for k in range(1, measures 1) for n in [k] list(range(2, beats))
))
Output:
1 2 3 4 2 2 3 4 3 2 3 4 4 2 3 4