Multiplication of tables to print on the screen/console when given the arguments

Hi

I wrote a program to give arguments to the multiply function t print the tables.

def multipy(*args):
        """displaying the tables with the arguments supplied"""
         for j in range(1,11):
             for i in args:
                 print(f'{i} X {j:>2} = {i*j:>2}', end='\t')
             print()

multiply (2,3,6,9)

this works for less arguments like “multiply(2,4,5,17)”

But this is not working when i give either many arguments or floating point numbers or bigger numbered tables.

like multiply(2.5,34.2,38.5) or multiply(2,4,5,6,12,15,19,22) or multiply(234,2435,734)

Can anyone guide me here.

Regards,
Suresh

Sample output …

In [70]: multipy(2,4,6,7,19,23,25,28,12)
2 X  1 =  2     4 X  1 =  4     6 X  1 =  6     7 X  1 =  7     19 X  1 = 19    23 X  1 = 23    25 X  1 = 25    28 X  1 = 28 12 X  1 = 12
2 X  2 =  4     4 X  2 =  8     6 X  2 = 12     7 X  2 = 14     19 X  2 = 38    23 X  2 = 46    25 X  2 = 50    28 X  2 = 56 12 X  2 = 24
2 X  3 =  6     4 X  3 = 12     6 X  3 = 18     7 X  3 = 21     19 X  3 = 57    23 X  3 = 69    25 X  3 = 75    28 X  3 = 84 12 X  3 = 36
2 X  4 =  8     4 X  4 = 16     6 X  4 = 24     7 X  4 = 28     19 X  4 = 76    23 X  4 = 92    25 X  4 = 100   28 X  4 = 112        12 X  4 = 48
2 X  5 = 10     4 X  5 = 20     6 X  5 = 30     7 X  5 = 35     19 X  5 = 95    23 X  5 = 115   25 X  5 = 125   28 X  5 = 140        12 X  5 = 60
2 X  6 = 12     4 X  6 = 24     6 X  6 = 36     7 X  6 = 42     19 X  6 = 114   23 X  6 = 138   25 X  6 = 150   28 X  6 = 168        12 X  6 = 72
2 X  7 = 14     4 X  7 = 28     6 X  7 = 42     7 X  7 = 49     19 X  7 = 133   23 X  7 = 161   25 X  7 = 175   28 X  7 = 196        12 X  7 = 84
2 X  8 = 16     4 X  8 = 32     6 X  8 = 48     7 X  8 = 56     19 X  8 = 152   23 X  8 = 184   25 X  8 = 200   28 X  8 = 224        12 X  8 = 96
2 X  9 = 18     4 X  9 = 36     6 X  9 = 54     7 X  9 = 63     19 X  9 = 171   23 X  9 = 207   25 X  9 = 225   28 X  9 = 252        12 X  9 = 108
2 X 10 = 20     4 X 10 = 40     6 X 10 = 60     7 X 10 = 70     19 X 10 = 190   23 X 10 = 230   25 X 10 = 250   28 X 10 = 280        12 X 10 = 120

What do you mean by it’s not working?

I think the end = '\t' that seperates your tables is the problem.
Don’t use it for formatting purposes, it’s difficult to know how much whitespaces you’ll end up with between the tables.

In your function here it adds 1 space when the line currently ends at 124 letters (i.e. after 28 x 1 = 28) and 8 spaces when it ends at 125 letters(the other lines). When changing {i*j:>2} to {i*j:>3} your example is okay, but when using the function for numbers bigger than 100 the error will be back.

I suggest you adjust {i*j:>2} depending on how many digits i has.
And for consistency replace end='\t' with a couple of whitespaces, then you can be sure that on every platform your output is the same :slightly_smiling_face:

Could you suggest me how to do it?

I don’t know whether there is a prebuilt function for that…but you can always write your own function for it, like this one here:

def format(i,j):
  if len(str(i*j)) == len(str(i*10)):
    return i*j
  else:
    return " " + str(i*j)

This one only works if i is an int, and only for j = 1…10 (1 space difference max)
For floats you have to additionally make sure every number has the same number of decimal places (use instanceof(i, int) to check if you need decimal places and add that case to the format-function)

Then you can use the following line in the multiply function for every number you want to multiply
print(f'{i} X {j:>2} = {format(i,j)}', end=' ')

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.