mavocado
mavocado

Reputation: 47

how to make a multiplication table in python

I'm learning python and I face this problem, i want to make a multiplication table like this

 1 2 3
 2 4 6
 3 6 9

I got this code: n = 4

rango = range(1,n)

for i in rango:
   for j in rango:
   print rango[j-1] * i,

but the output is this:

1 2 3 2 4 6 3 6 9

I'm making this script in python 2.7

P.S: Sorry for my english, english is not my native language

Upvotes: 2

Views: 1216

Answers (2)

intboolstring
intboolstring

Reputation: 7110

This would work to print a multiplication table

n=10
for a in range(1,n):
    val = ""
    for b in range(1,n):
        val = val + str(a*b) + " "
        print(val)

or modifying you way

rango = range(1,n)
for i in rango:
    for j in rango:
        print rango[j-1] * i,
    print

Edit: Apologies if my post is similar to another. I was writing this as others were posted.

Upvotes: 1

wim
wim

Reputation: 363133

Just print a new line after each row:

for i in rango:
    for j in rango:
        print rango[j-1] * i,
    print

However I should point out that this would be clearer:

for i in rango:
    for j in rango:
        print j * i,
    print

Upvotes: 2

Related Questions