2

I need to know how to generate a sequence of numbers that are right-justified in rows (of 6) and columns (of 7) with a set field width (of 2) and space (of 1).

First I have to ask the user for an input number "n" (with requirements -6 < n < 2) and then print "n to n+6" in the first column, "n+7 to n+13" in the following column and so on.

I've got the input/output from the following code but don't know how to generate the rows and columns (or specify the field widths):

n = int(input("Enter the start number: "))

if n>-6 and n<2:
    for x in range(n, n+41):
        print(n, end=" ")
        n = n+1

I've been trying to do it with for loops within this for loop but can't figure it out. Any help will be really appreciated - thank you in advance!

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
Josh Alexandre
  • 127
  • 3
  • 3
  • 11

3 Answers3

2

The highest number can only have two digits so you just need to right align 2, using a step of 6 with the first loop and starting the inner loop from each x from the first, we also need to catch when n is equal to -5, 38 with a step of six always gives us 7 columns and six rows unless n is -5 then we need to use 37 so minus (n < -4) would make 38 -> 37 when n was -5 or else take nothing away:

n = int(input("Enter the start number: "))

if -6 < n < 2:
    for x in range(n, 38 - (n < -4), 6):
        for j in range(x,  x + 6):
            print("{:>2}".format(j), end=" ")
        print()

Putting it in a function pr_right and running from -5 to 1:

-5 -4 -3 -2 -1  0 
 1  2  3  4  5  6 
 7  8  9 10 11 12 
13 14 15 16 17 18 
19 20 21 22 23 24 
25 26 27 28 29 30 
31 32 33 34 35 36 

-4 -3 -2 -1  0  1 
 2  3  4  5  6  7 
 8  9 10 11 12 13 
14 15 16 17 18 19 
20 21 22 23 24 25 
26 27 28 29 30 31 
32 33 34 35 36 37 

-3 -2 -1  0  1  2 
 3  4  5  6  7  8 
 9 10 11 12 13 14 
15 16 17 18 19 20 
21 22 23 24 25 26 
27 28 29 30 31 32 
33 34 35 36 37 38 

-2 -1  0  1  2  3 
 4  5  6  7  8  9 
10 11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 
28 29 30 31 32 33 
34 35 36 37 38 39 

-1  0  1  2  3  4 
 5  6  7  8  9 10 
11 12 13 14 15 16 
17 18 19 20 21 22 
23 24 25 26 27 28 
29 30 31 32 33 34 
35 36 37 38 39 40 

 0  1  2  3  4  5 
 6  7  8  9 10 11 
12 13 14 15 16 17 
18 19 20 21 22 23 
24 25 26 27 28 29 
30 31 32 33 34 35 
36 37 38 39 40 41 

 1  2  3  4  5  6 
 7  8  9 10 11 12 
13 14 15 16 17 18 
19 20 21 22 23 24 
25 26 27 28 29 30 
31 32 33 34 35 36 
37 38 39 40 41 42 

There are other and easier ways but I imagine this is some kind of learning exercise.

If it is actually six rows and 7 columns that is easier:

for x in range(n, 37, 7):
    for j in range(x,  x + 7):
        print("{:>2}".format(j), end=" ")
    print()

Which if we run it through another pr_right function outputs:

In [10]: for n in range(-5, 2):
             pr_right(n)
             print()
   ....:     
-5 -4 -3 -2 -1  0  1 
 2  3  4  5  6  7  8 
 9 10 11 12 13 14 15 
16 17 18 19 20 21 22 
23 24 25 26 27 28 29 
30 31 32 33 34 35 36 

-4 -3 -2 -1  0  1  2 
 3  4  5  6  7  8  9 
10 11 12 13 14 15 16 
17 18 19 20 21 22 23 
24 25 26 27 28 29 30 
31 32 33 34 35 36 37 

-3 -2 -1  0  1  2  3 
 4  5  6  7  8  9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 29 30 31 
32 33 34 35 36 37 38 

-2 -1  0  1  2  3  4 
 5  6  7  8  9 10 11 
12 13 14 15 16 17 18 
19 20 21 22 23 24 25 
26 27 28 29 30 31 32 
33 34 35 36 37 38 39 

-1  0  1  2  3  4  5 
 6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28 29 30 31 32 33 
34 35 36 37 38 39 40 

 0  1  2  3  4  5  6 
 7  8  9 10 11 12 13 
14 15 16 17 18 19 20 
21 22 23 24 25 26 27 
28 29 30 31 32 33 34 
35 36 37 38 39 40 41 

 1  2  3  4  5  6  7 
 8  9 10 11 12 13 14 
15 16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 
36 37 38 39 40 41 42 
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

You can use numpy for reshape. This gives a concise and more readable code:

import numpy as np

n = int(input("Enter the start number: "))

if n>-6 and n<2:
    print (n + np.arange(42)).reshape(6,7)
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
0

Version without numpy:

n = int(input("Enter the start number: "))

for x in range(n, n + 42, 7):
    for y in range(x, x + 7):
        print y,
    print
Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24