用python打印乘法表

用python打印乘法表,python,multiple-columns,nested-loops,Python,Multiple Columns,Nested Loops,我决定使用更复杂的策略来实践嵌套for循环逻辑,尽管有几种更简单的方法。我是一个初学者,所以我希望我的文档不会让我的思维过程混乱 import sys matrix = [1,2,3,4,5,6,7,8,9,10,11,12] #This forloop will multiply the matrix from 1 to 12 for multiplier in range(1,13): #This forloop will increment and shift the view

我决定使用更复杂的策略来实践嵌套for循环逻辑,尽管有几种更简单的方法。我是一个初学者,所以我希望我的文档不会让我的思维过程混乱

import sys
matrix = [1,2,3,4,5,6,7,8,9,10,11,12]

#This forloop will multiply the matrix from 1 to 12
for multiplier in range(1,13):
    #This forloop will increment and shift the view to each matrix cell from [0] to [11]
    for counter in range(0,12):

        #This will multiple every row by 'multiplier'
        #matrix[0]*1,matrix[1]*1,matrix[2]*1...matrix[11]*1
        #matrix[0]*2 ...                    ...matrix[11]*2
        #   .   .                                   .
        #   .               .                       .
        #   .                               .       .
        #matrix[0]*12...                    ...matrix[11]*12

        sys.stdout.write(str(matrix[counter]*multiplier))

        #Since each number (is) formatted to a width of 4' then 1 digit numbers will
        #have 3 spaces, 2 digit numbers will have 2 spaces, 3 digit numbers will have
        #only 1 space left. So the length of the numbers will be called and subtracted
        #from 4 to create the appropriate amount of spaces. Example will be:
        #
        #3   6   9  12  15  18  21  24  27  30  33  36 (there are only 2 spaces between 12 and 15)
        #12  24  36 48  60  72  84  96 108 120 132 144 (there is only 1 space between 108 and 120)
        sys.stdout.write(int(4)-len(matrix[counter])*+" ")

    #Adds a new line after finishing a row
    print("")
这是我当前的输出:

Traceback (most recent call last):
  File "<File location>", line 29, in <module>
    sys.stdout.write(int(4)-len(matrix[counter])*+" ")
TypeError: object of type 'int' has no len()
1
Process finished with exit code 1
这给了我这个错误:

Traceback (most recent call last):
1  File "<File location>", line 29, in <module>
    sys.stdout.write(int(4)-len(int(matrix[counter]))*+" ")
TypeError: object of type 'int' has no len()

Process finished with exit code 1
输出:

   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
  10  20  30  40  50  60  70  80  90 100 110 120
  11  22  33  44  55  66  77  88  99 110 121 132
  12  24  36  48  60  72  84  96 108 120 132 144

Process finished with exit code 0

尝试将
str
强制转换为整数,如下所示:

sys.stdout.write(int(4)-len(str(matrix[counter]))*+" ")

实际上Python可以为您处理字符串格式。用以下代码替换
sys.stdout.write

 sys.stdout.write( '{:4d}'.format(matrix[counter]*multiplier) )
此代码段使用了Python 2.6中引入的新字符串格式化机制。在本例中,我们告诉Python输出限制为特定宽度的十进制整数(
d


中介绍了字符串格式语法。不过,我建议您在站点上查看一组完整的示例:
sys.stdout.write(int(4)-len(str(矩阵[counter]))*+“”)类型错误:一元数+的操作数类型错误:'str'
进程已完成,退出代码1无效!你能给我解释一下语法吗?
sys.stdout.write(int(4)-len(str(matrix[counter]))*+" ")
 sys.stdout.write( '{:4d}'.format(matrix[counter]*multiplier) )