0

I am trying to write a function that given a number for n it will return individual multiplication tables up to n+1 and the range of each table is also n+1. I have got the code write to get what I am looking for but I don't know how to make separate tables, my teacher has a gift for asking for things in the homework that aren't in the lecture.

[IN]:

n = 4

def multitables(n):
    for i in range(1, n+1):
        for j in range(1, n+1):
            print ('{0}  {1}  {2}'.format(i, j, i*j))
        
print(multitables(n))

[OUT]:
1  1  1
1  2  2
1  3  3
1  4  4
1  5  5
2  1  2
2  2  4
2  3  6
2  4  8
3  1  3
3  2  6
3  3  9
3  4  12
4  1  4
4  2  8
4  3  12
4  4  16

How do I get it to make multiple tables side by side for each iteration of i So instead I would have:

[IN]:print(multitable(4))   
[OUT]:

1 1 1   2 1 2   3 1 3   4 1 4 
1 2 2   2 2 4   3 2 6   4 2 8
1 3 3   2 3 6   3 3 9   4 3 12
1 4 4   2 4 8   3 4 12  4 4 16

Thank you

Rachel Cyr
  • 429
  • 1
  • 5
  • 15

1 Answers1

0

If I understood you correctly:

def table(n):
    for limit in range(1, n+1):    
        s = ""
        for i in range(1, limit):
            for j in range(1, limit):
                s += f"{i*j}\t"
            s += "\n"

        print(s)
table(6)

#1  

#1  2   
#2  4   

#1  2   3   
#2  4   6   
#3  6   9   

#1  2   3   4   
#2  4   6   8   
#3  6   9   12  
#4  8   12  16  

#1  2   3   4   5   
#2  4   6   8   10  
#3  6   9   12  15  
#4  8   12  16  20  
#5  10  15  20  25  
BrandyBerry
  • 136
  • 4
  • You kind of got it but I think its my fault because I didn't explain correctly. But this could is helpful for to get different tables as opposed to all one. I don't know if you saw the second "out" in my post but that's more the format I am looking for: – Rachel Cyr Nov 03 '20 at 21:36