How to generate rows and columns in Python 3
Solution 1
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
Solution 2
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)
Related videos on Youtube
Josh Alexandre
Updated on September 15, 2022Comments
-
Josh Alexandre over 1 year
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!
-
Josh Alexandre about 8 yearsThanks for the answer @Padraic. I actually had my rows and columns mixed up but managed to use your advice and correct it with:
if n>-6 and n<2: for x in range(n, 35 + n, 6): for j in range(x, x + 7): print("{:>2}".format(j), end=" ") print()
-
Padraic Cunningham about 8 years@JoshAlexandre, no worries, so seven rows and six columns?
-
Josh Alexandre about 8 yearsThanks @Colonel. I went with using a for loop within another for loop but appreciate learning about numpy! Will definitely use it in the future.
-
Josh Alexandre about 8 yearsThanks for the help @Maciej!
-
Josh Alexandre about 8 yearsYep, all sorted though. Thank you!
-
Padraic Cunningham about 8 years@JoshAlexandre, I added an example for seven rows and six columns, I think your implementation might not be quite right
-
Colonel Beauvel about 8 yearsit is vectorized and much faster/neater than for loops!