在 python 中打印乘法 table
Printing out multiplication table in python
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
尝试将 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
)。
documentation. However I recommend you to take a look at pyformat.info 站点中描述了字符串格式化语法以获得一组干净的示例
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
尝试将 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
)。
documentation. However I recommend you to take a look at pyformat.info 站点中描述了字符串格式化语法以获得一组干净的示例