0

Learning python and I need to figure out to to increase the first number in a range?


measures = 4
beats_per_measures = 4
beats = 0

for i in range(0, measures):
    for beats in range(1 , beats_per_measure + 1):
        print(beats)

so I can get the output to print the beats per measure the number of measures. BOOm. but now I need to increase the first beat by one every measure and I know it's easier than I am making it. I have preformed multiple searches online and I am guessing that because I don't know how to read the complex answers or I don't know how to phrase what I am trying to do is why I have not found an answer yet.

  • So you want beats to start with 1 and not 0 ? – Metapod Oct 06 '20 at 15:55
  • Yes. So it would print out (on separate lines) 1 2 3 4 then 2 2 3 4, 3 2 3 4, 4 2 3 4 and the first number will increase for the number of measures while the other number stay the same. – Smash Adams Oct 06 '20 at 16:56

4 Answers4

0

Is this what you need?

I thought you needed each set (1,2,3,4) in the same line.

measures = 4
beats_per_measure = 4

for i in range(measures):
    for var_beat in range(beats_per_measure):
        var_beat = str(1 + var_beat)
        fixed_beat = ' '.join([ str(1+f) for f in range(beats_per_measure)[1:]])
        print(var_beat + ' ' + fixed_beat)

I am a little bit confused with your "so on" in your comment.

O Pardal
  • 647
  • 4
  • 21
0

From your comment, you just want to always print an increasing number followed by 2, 3, 4. So just print the first beat manually based on the measure you're currently processing:

for measure in range(1, measures+1):  # Adjust bounds so we don't have to add one inside the loop
    print(measure)    # Prints the changing value
    for beats in range(2, beats_per_measure + 1):
        print(beats)  # Prints the consistent 2, 3, 4 values

If you want each measure on the same line, the minimal change is to just add end='' or end=' ' to each print and add an empty print() to the end of the outer loop.

A cleaner solution would be to just print each measure all at once; you can unpack a range argument with * to make it operate as separate arguments to print, and use sep to provide the characters you want in between, getting something like:

for measure in range(1, measures + 1):
    print(measure, *range(2, beats_per_measure + 1), sep=', ')
# prints:
1, 2, 3, 4
2, 2, 3, 4
3, 2, 3, 4
...

and it's trivial to change the separation to just a single space, sep=' ', or no separation at all, sep=''.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Sorry. one number per line and the only number that increases is the first. so 1234, 2234, 3234, 4234, 5234. the first number would almost or would be the count ultimately finishing equal to the measures? I hope that makes more sense. – Smash Adams Oct 06 '20 at 17:02
  • @MichaelJAdams: Not even space separated? So just `for i in range(1234, measures * 1000 + 235, 1000): print(i)` would do it for you (though it would get a little confusing is `measures >= 10`)? – ShadowRanger Oct 06 '20 at 18:00
  • @MichaelJAdams: Added a solution that does exactly that (well, it includes commas and spaces between the numbers, but the final line explains how to get rid of them if that's what you want). – ShadowRanger Oct 06 '20 at 18:14
0

You can do:

for e in range(1,5):
    print(', '.join(map(str, ([e]+list(range(2,5))))))

Prints:

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

Or, perhaps:

>>> print('\n'.join([f"{e}, 2, 3 ,4" for e in range(1,5)]))
# same output

Which could be made dynamic by this:

measures = 4
beats_per_measures = 4

rest_of_measure=', '.join(f'{e}' for e in range(2,beats_per_measures+1))
print('\n'.join([f"{e}, {rest_of_measure}" for e in range(1, measures+1)]))
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Minor improvement on explicitly `list`ifying a `range` (which always makes me squirm) only to concatenate it with another `list` and throw the temporaries away. Use generalized unpacking to directly construct the `list` you want, replacing `print(', '.join(map(str, ([e]+list(range(2,5))))))` with `print(', '.join(map(str, [e, *range(2,5)])))`. Or just let `print` with a `sep` do the work as in my answer (as it avoids the need for conversion to `str`). – ShadowRanger Oct 06 '20 at 18:13
0
measures = 4
beats_per_measure = 4


for measure in range(0 , measures):
    for beats in range(1 , beats_per_measure + 1):
        #print(beats)
        if beats == 1:
            beats += measure
        elif beats != 1:
            beats = beats
        print(beats)

this is the answer I was looking for. It was a ^%^%$ conditional that helped me do it!!! Thanks for all the help can't wait to plug in all of your recommendations to see what else I can do!