打印 n * n 的乘法表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #打印 9*9 乘法表 2 def Multiplication(n): 3 # n - 定义打印的行数 4 max_len = len(str((n)**2)) #计算最大值的占位(用于打印时输出更好看) 5 for row in range(1,n+1): 6 for col in range(1,row+1): 7 res = str(row * col) 8 print( '{0}'.format(res).rjust(max_len),end=" ") #打印一行 9 print("")#打印完一行后换行 10 11 #指定要打印的行数并输出 12 n = 9 13 Multiplication( n )
输出结果:
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81