简体   繁体   English

连续数字的乘法表 Python

[英]Multiplication Table for Contiguous Numbers Python

The output should look like this table .输出应如下表所示 It is a multiplication table of 9 rows times 12 columns (technically 13).它是一个 9 行乘以 12 列(技术上为 13)的乘法表。

The first column is 10 characters wide and is left aligned.第一列宽 10 个字符,左对齐。 All other columns are five characters wide and are right-aligned.所有其他列的宽度为五个字符且右对齐。

This is my code so far but I'm trying to format the multiplication table to be in the same rows as everything else到目前为止,这是我的代码,但我正在尝试将乘法表的格式设置为与其他所有内容位于同一行

I am not allowed to use any external modules such as itertools or functools我不允许使用任何外部模块,例如 itertools 或 functools

 print(f"{'Column':<10s}", end = '\t'),
    for x in range(1,13):
        print(f"{x:5}", end='')


print(f"{'Column':<10s}", end = '\t'),
for x in range(1,13):
    print(f"{x:>5}", end='')

print() 
for x in range(1,10):
    print('Row',f'{x:<10}')


for row in range (1, 10):
    for col in range(1,13):
        print(f'{row*col:>5}', end="\t")

This is what I get:这就是我得到的:

Column          1    2    3    4    5    6    7    8    9   10   11   12
Row 1         
Row 2         
Row 3         
Row 4         
Row 5         
Row 6         
Row 7         
Row 8         
Row 9         
    1       2       3       4       5       6       7       8       9      10      11      12       2       4       6       8      10      12      14      16      18      20      22      24       3       6       9      12      15      18      21      24      27      30      33      36       4       8      12      16      20      24      28      32      36      40      44      48       5      10      15      20      25      30      35      40      45      50      55      60       6      12      18      24      30      36      42      48      54      60      66      72       7      14      21      28      35      42      49      56      63      70      77      84       8      16      24      32      40      48      56      64      72      80      88      96       9      18      27      36      45      54      63      72      81      90      99     108   

The cycle on cols should be nested inside the one in rows cols 上的循环应该嵌套在 row 中

for x in range(1,10):
    print('Row',f'{x:<10}', end="\t")
    for col in range(1,13):
        print(f'{row*col:>5}', end="\t")

Better do this:最好这样做:

for row in range(1,10):
    print('Row',f'{row:<10}', end="\t")
    print(*[str(row*col) for col in range(1,13)], sep = "\t", end="\n")

For @jumifiki's answer, if you don't understand the * in front of the list (which I don't either, actually), you can use the join method:对于@jumifiki 的回答,如果您不理解列表前面的* (实际上我也不理解),您可以使用join方法:

for row in range(1,10):
    print('Row',f'{row:<10}', end="\t\t")
    print('\t\t'.join([str(row*col) for col in range(1,13)]), end="\n")

Also, to make it even smaller you could use this:另外,为了让它更小,你可以使用这个:

for row in range(1,10):
    print(f'Row {row:<10} \t\t', '\t\t'.join([str(row*col) for col in range(1,13)]), end="\n")

You could even do a one-liner!!!你甚至可以做一个单线!!!

print('\n'.join([f'Row {row:<10} \t\t ' + '\t\t'.join([str(row*col) for col in range(1,13)]) for row in range(1, 10)]))

NOTE: At the top of the output, since there is supposed to be a Column , you could use this for that part:注意:在输出的顶部,因为应该有一个Column ,您可以将其用于该部分:

print(f"{'Column':<12} ", end='\t\t ') # Use <12 becuse we have 2 more characters than each row label.
for c in range(1, 13):
    print(c, end="\t\t")
print()

Or, a one-liner:或者,单线:

print(f"{'Column':<12} \t\t", '\t\t'.join(map(str, range(1, 13))))

If you want, you can combine both of these print statements into ONE print statement so the whole thing is printed by ONE LINE!!如果你愿意,你可以将这两个打印语句组合成一个打印语句,这样整个事情就被一行打印了!! (this is kinda crazy) (这有点疯狂)

print(f"{'Column':<12} \t\t", '\t\t'.join(map(str, range(1, 13))) + '\n' + '\n'.join([f'Row {row:<10} \t\t ' + '\t\t'.join([str(row*col) for col in range(1,13)]) for row in range(1, 10)]))

(I suggest you don't use this, just for code readability) (我建议你不要使用这个,只是为了代码可读性)

Hope this helped you!希望这对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM