gpt4 book ai didi

python - 在python中打印乘法口诀表

转载 作者:太空宇宙 更新时间:2023-11-03 17:45:44 25 4
gpt4 key购买 nike

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

最佳答案

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

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

此代码片段使用了 Python 2.6 中引入的新字符串格式化机制。在本例中,我们告诉 Python 输出限制为特定宽度的十进制整数 (d)。

字符串格式化语法在 documentation 中描述。 。不过我建议您看看pyformat.info网站提供一组干净的示例

关于python - 在python中打印乘法口诀表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com