python如何打印乘法表_用python打印乘法表
我決定用更復雜的策略來實踐我的嵌套for循環邏輯,盡管有幾種更簡單的方法。我是一個初學者,所以我希望我的文檔不會混淆我的思維過程。在
^{1}$
以下是我當前的輸出:
^{pr2}$
我嘗試了一些快速修復方法,比如將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 "", line 29, in
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如何打印乘法表_用python打印乘法表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: informix 外部表
- 下一篇: websocket python爬虫_p