今天主要介绍如何用python实现九九乘法表(9*9 乘法口诀表),下面一起来看看吧~
思路:
分行与列考虑,共9行9列,i控制行,j控制列。
实现方法
#!/usr/bin/python
#用Python实现九九乘法表(9*9 乘法口诀表)
#分行与列考虑,共9行9列,i控制行,j控制列
#方法一采用for循环
#后面的方法采用函数和for循环结合
print ("====================方法一====================")
for i in range(1, 10):
for j in range(1,i 1):
d=i*j
print('%d*%d=%-2d'%(i,j,d),end=' ')
print ()
print ("====================方法二====================")
def hwb(n):
m=n
sums=0
for j in range(1, n 1):
sums=m*j
print('%d*%d=%-2d'%(m,j,sums),end=' ')
print ()
def hwb1():
for i in range(1,10):
hwb(i)
hwb1()
print ("====================方法三====================")
def hwb(n):
m=n
sums=0
for j in range(1, n 1):
sums=m*j
print('%d*%d=%-2d'%(m,j,sums),end=' ')
print()
def hwb2():
for i in range(9,0,-1):
hwb(i)
hwb2()
print ("====================方法四====================")
def hwb(n):
m=n
sums=0
for k in range(0, 9-n):
print(' ',end='')
for j in range(1, n 1):
sums=m*j
print('%d*%d=%-2d'%(m,j,sums),end=' ')
print()
def hwb3():
for i in range(1,10):
hwb(i)
hwb3()
print ("====================方法五====================")
def hwb(n):
for dix in range(9-n,0,-1):
print(' ',end = '')
sums = 0
m = n
for j in range(1,n 1):
sums = m*j
print("%d*%d=%-2d"%(m,j,sums),end = ' ')
print()
def hwb4():
for i in range(9,0,-1):
hwb(i)
hwb4()
输出结果:
觉得有用的朋友多帮忙转发哦!后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~