在python中打印出乘法表

时间:2015-04-24 04:23:22

标签: python multiple-columns nested-loops

Currently I'm doing this problem and I've ran into errors.

我决定用一些更复杂的策略练习我的嵌套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

我尝试了快速修复,例如将len(matrix [counter])更改为int,但会产生相同的错误消息。

我也试过以下

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

这给了我这个错误:

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

最终解决方案:

import sys
matrix = [1,2,3,4,5,6,7,8,9,10,11,12]
for multiplier in range(1,13):
    for counter in range(0,12):
        sys.stdout.write('{:>4}'.format( str(matrix[counter]*multiplier) ))
    print("")

输出:

   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

2 个答案:

答案 0 :(得分:1)

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

 sys.stdout.write( '{:4d}'.format(matrix[counter]*multiplier) )

此代码段使用新的字符串格式化机制,该机制在Python 2.6中引入。在这种情况下,我们告诉Python输出约束到特定宽度的十进制整数(d)。

字符串格式化语法在documentation中描述。不过,我建议您查看pyformat.info网站以获取一组简洁的示例

答案 1 :(得分:0)

尝试将loginUrl转换为整数:

json result