python 乘法循环_Python中的九九乘法表(for循环)

用for循环写出的九九乘法表(包括函数的调用)

#方向一

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()

下图为上面代码运行结果:

以下为函数练习,函数和for循环结合的九九乘法表(没有上面的简洁明了)

#方向一

def hanshu(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 hanshu1():

for i in range(1,10):

hanshu(i)

hanshu1()

下图为上面代码运行结果:

#方向二

def hanshu(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 hanshu1():

for i in range(9,0,-1):

hanshu(i)

hanshu1()

下图为上面代码运行结果:

#方向三

def hanshu(n):

m = n

sums = 0

for k in range(0,10-n):

print("       ",end = "")

for j in range(1,n+1):

sums = m*j

print("%d*%d=%-2d"%(m,j,sums),end = " ")

print("")

def hanshu1():

for i in range(1,10):

hanshu(i)

hanshu1()

下图为上面代码运行结果:

#方向四

def hanshu(n):

for dix in range(10-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 hanshu1():

for i in range(9,0,-1):

hanshu(i)

hanshu1()

下图为上面代码运行结果:

你可能感兴趣的:(python,乘法循环)