Question

I am trying to learn nested loops so I want to make a simple multiplication table, could you please help me improve my code?

for i in range(1,10):
    print("i =", i, ":")
    for j in range(1, 10):
        print (i*j)

My program looks very messy when I run it, how can I align the results in something like a matrix/table to make it easier to read?

Était-ce utile?

La solution

If you are using Python 2.x, print is a statement, so you need to make sure that the line doesn't end after printing the current line. {:2d} in the format function makes sure that you have always have space for two digits. If you prefer 0 instead of spaces you can use {:02d}.

for i in range(1, 10):
    print "i =", i, ":",                   # Note the comma at the end
    for j in range(1, 10):
        print "{:2d}".format(i * j),       # Note the comma at the end
    print

Output

i = 1 :  1  2  3  4  5  6  7  8  9
i = 2 :  2  4  6  8 10 12 14 16 18
i = 3 :  3  6  9 12 15 18 21 24 27
i = 4 :  4  8 12 16 20 24 28 32 36
i = 5 :  5 10 15 20 25 30 35 40 45
i = 6 :  6 12 18 24 30 36 42 48 54
i = 7 :  7 14 21 28 35 42 49 56 63
i = 8 :  8 16 24 32 40 48 56 64 72
i = 9 :  9 18 27 36 45 54 63 72 81

Small change in the print, can make it work in Python 3.x, as well. Remember print is a function in Python 3.x

for i in range(1, 10):
    print("i =", i, ":", end=" ")
    for j in range(1, 10):
        print("{:2d}".format(i * j), end=" ")
    print()

Note: You can read more about the various string formatting options, in the Format Specification Mini-Language section of the Python docs.

Autres conseils

Do you want something like this?:

for i in range(1, 10):
    print '\n','{:02}'.format(i),':',
    for j in range(1, 10):
        print '{:02}'.format(i*j),

[OUTPUT]
01 : 01 02 03 04 05 06 07 08 09 
02 : 02 04 06 08 10 12 14 16 18 
03 : 03 06 09 12 15 18 21 24 27 
04 : 04 08 12 16 20 24 28 32 36 
05 : 05 10 15 20 25 30 35 40 45 
06 : 06 12 18 24 30 36 42 48 54 
07 : 07 14 21 28 35 42 49 56 63 
08 : 08 16 24 32 40 48 56 64 72 
09 : 09 18 27 36 45 54 63 72 81

OR:

for i in range(1, 10):
    for j in range(1, 10):
        print '{:02}'.format(i),'X','{:02}'.format(j),'=','{:02}'.format(i*j)
    print

[OUTPUT]
01 X 01 = 01
01 X 02 = 02
01 X 03 = 03
01 X 04 = 04
01 X 05 = 05
01 X 06 = 06
01 X 07 = 07
01 X 08 = 08
01 X 09 = 09

02 X 01 = 02
02 X 02 = 04
02 X 03 = 06
02 X 04 = 08
02 X 05 = 10
02 X 06 = 12
02 X 07 = 14
02 X 08 = 16
02 X 09 = 18

03 X 01 = 03
03 X 02 = 06
03 X 03 = 09
03 X 04 = 12
03 X 05 = 15
03 X 06 = 18
03 X 07 = 21
03 X 08 = 24
03 X 09 = 27

...
...
...
...

Try this

for a in range(1,6):
    print(a,'tables')
    for b in range(1,11):
        c=a*b
        print(a,'X',b,'=', c)
    print()

Just replace 6 by a number which you want and 1 to that (eg: if you 
want up to 10 replace 6 with 11)  

Output

1 tables
1 X 1 = 1
1 X 2 = 2
1 X 3 = 3
1 X 4 = 4
1 X 5 = 5
1 X 6 = 6
1 X 7 = 7
1 X 8 = 8
1 X 9 = 9
1 X 10 = 10

2 tables
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 = 20

3 tables
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
3 X 5 = 15
3 X 6 = 18
3 X 7 = 21
3 X 8 = 24
3 X 9 = 27
3 X 10 = 30

4 tables
4 X 1 = 4
4 X 2 = 8
4 X 3 = 12
4 X 4 = 16
4 X 5 = 20
4 X 6 = 24
4 X 7 = 28
4 X 8 = 32
4 X 9 = 36
4 X 10 = 40

5 tables
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top