如何将10 * 10倍的表格打印为网格? [英] How to print a 10*10 times table as a grid?

查看:51
本文介绍了如何将10 * 10倍的表格打印为网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用for循环打印10x10倍的表格.

I am trying to print a 10x10 times table using for loops.

这是我的尝试:

for x in range (1, 11):
    for y in range (1, 11):
        print (x*y)
    print()

输出是数字的垂直线.我像方桌一样需要它.

The output is a vertical line of numbers. I need it like the square table kind.

推荐答案

您需要做的就是利用 end 参数:

What you need to do is leverage the end argument:

for x in range (1, 11):
    for y in range (1, 11):
        print ('{:3}'.format(x*y), end=' ')
    print()

此外,请注意行条目的格式化方式.通过使用'{:3}'.format(x * y),用三位空格填充表达式.有关格式的更多详细信息,请咨询文档.

Also, note the way the row entries are formatted. By using '{:3}'.format(x*y), the expression is padded with spaces out to three digits. For more details on formatting, consult the documentation.

示例输出:

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

这篇关于如何将10 * 10倍的表格打印为网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆