Skip to content Skip to sidebar Skip to footer

How To Print In Range?

how would I print something like that in python? This is what I could do until now: for i in range(24): print('('+str(i)+', '+str(i)+'):',i, end='')

Solution 1:

The following:

i = 0
for y in range(0, 500, 125):
   for x in range(0, 750, 125):
      print('(%3d, %3d): %3d    ' % (x, y, i), end='')
      i += 1
   print()

produces

(  0,   0):   0     (125,   0):   1     (250,   0):   2     (375,   0):   3     (500,   0):   4     (625,   0):   5    
(  0, 125):   6     (125, 125):   7     (250, 125):   8     (375, 125):   9     (500, 125):  10     (625, 125):  11    
(  0, 250):  12     (125, 250):  13     (250, 250):  14     (375, 250):  15     (500, 250):  16     (625, 250):  17    
(  0, 375):  18     (125, 375):  19     (250, 375):  20     (375, 375):  21     (500, 375):  22     (625, 375):  23    

Alternatively, with a single loop:

for i in range(24):
   print('(%3d, %3d): %3d    ' % (i % 6 * 125, i // 6 * 125, i), end='')
   if (i + 1) % 6 == 0:
      print()

Solution 2:

from __future__ import print_function
from itertools import count
counter = count()
for i in range(4):
    for j in range(6):
        print ((j*125,i*125),':',next(counter),end='\t')
    print()

You can use string formatting to get the alignment of text that you want.


Post a Comment for "How To Print In Range?"