如何使我的乘法表更整洁?

时间:2018-10-01 14:51:52

标签: python

我创建的乘法没有很好地格式化/组织-我希望它具有分隔数字的行。

我的代码:

n = int(input("Enter a positive interger between 1 and 9: "))
for row in range(1, n+1):
    print(*(f"{row*col:5}" for col in range(1, n+1)))

然后它将显示以下内容:

Enter a positive integer between 1 and 9: 9

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

但是我想要这样的东西:

Enter a positive integer between 1 and 9: 9
    1     2     3     4     5     6     7     8     9
------------------------------------------------------
    2  |   4     6     8    10    12    14    16    18
    3  |   6     9    12    15    18    21    24    27
    4  |   8    12    16    20    24    28    32    36
    5  |  10    15    20    25    30    35    40    45
    6  |  12    18    24    30    36    42    48    54
    7  |  14    21    28    35    42    49    56    63
    8  |  16    24    32    40    48    56    64    72
    9  |  18    27    36    45    54    63    72    81

3 个答案:

答案 0 :(得分:1)

使用tabulate

import pandas as pd
from tabulate import tabulate
n = int(input("Enter a positive interger between 1 and 9: "))

data = []

for row in range(1, n+1):
    tmp = [row*col for col in range(1, n+1)]
    data.append(tmp)

df = pd.DataFrame(data, columns=[*(range(1, n+1))])
df.loc[0] = df.loc[0]+1
print(tabulate(df,  headers= "firstrow", floatfmt=".0f", showindex=False))

打印输出:

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

答案 1 :(得分:0)

这是我的解决方法,

def pretty_num2str(N, MAX_DIGITS=2, EXTRA_SPACE=1):
    N = str(N)
    return " " * (MAX_DIGITS - len(N) + EXTRA_SPACE) + N + " |"

num = int(input("Enter a number between 0 and 9: "))

for row in range(1,num+1):
    line = ""
    footer = ""
    for col in range(1, num+1):
        pretty_string = pretty_num2str(row*col)
        line += pretty_string
        footer += "-" * (len(pretty_string)-1) + "+"
    print(line)
    print(footer)

输出:

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

整齐有序

答案 2 :(得分:0)

一个非常简单但不那么优雅的版本:

def format_row(lst, sep0='|', sep1=' ', fmt=' {:2d} '):
    strgs = (fmt.format(i) for i in lst[1:])
    return fmt.format(lst[0]) + sep0 + sep1.join(strgs)

def tabulate(n, sep='-'):
    first_row = format_row(range(1, n+1))
    l = len(first_row)
    ret = [first_row, l * sep]
    ret.extend(format_row(range(m, m*n+1, m)) for m in range(2, n+1))
    return '\n'.join(ret)

print(tabulate(3))

输出

  1 |  2    3 
--------------
  2 |  4    6 
  3 |  6    9 

(或者您是否希望在任何情况下都将行转到9?)