Python中的九九乘法表(for迴圈)

2022-08-19 13:18:14 字數 2069 閱讀 9375

用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九九乘法表

之前愛人用excel寫了九九乘法表 if and column a1 row a1 row a1 10 column a1 row a1 column a1 row a1 試試用python寫乙個九九乘法表。重構a a乘法表 函式 def jjcfb a for i in range 1,a 1 f...

Python 九九乘法表

以下例項演示了如何實現九九乘法表 coding utf 8 filename test.py author by www.runoob.com 九九乘法表 通過指定end引數的值,可以取消在末尾輸出回車符,實現不換行。for i in range 1 10 for j in range 1 i 1 ...

python 九九乘法表

詳解 為了讓自己理解 for i in range 1,10 print i,end for j in range 1,i 1 print s s s i,j,i j end print 1.for i in range 1,10 這是乙個for 迴圈 語句,range 是乙個函式,for i in...