def hanshu(n):#定義一個函數
m = n
sumn = 0 #設置一個變量,用來存儲兩個數相乘的積函數
#括號內包括第一個數,不包括第二數,so爲n+1學習
for dix in range(1,n+1): spa
sumn = m*dix3d
#%d -> 佔位符,%2d指佔兩個位置blog
#end=" "指不換行 for循環
print("%d*%d=%2d"%(m,dix,sumn),end = " ")
print("")
def hanshu1():
for b in range(1,10):
hanshu(b)
hanshu1()變量
效果圖:循環
def hanshu(n):
m = n
sumn = 0
for a in range(1,n+1):
sumn = m*a
print("%d*%d=%2d"%(m,a,sumn),end = " ")
print("")
def hanshu1():
for b in range(9,0,-1):
hanshu(b)
hanshu1()im
效果圖:db
def hanshu(n):
m = n
sumn = 0
for dix in range(0,10-n):
print(" ",end = "")#此處爲8個空格
for a in range(1,n+1):
sumn = m*a
print("%d*%d=%2d"%(m,a,sumn),end = " ")
print("")
def hanshu1():
for b in range(1,10):
hanshu(b)
hanshu1()
效果圖:
def hanshu(n):
for dix in range(10-n,0,-1):
print(" ",end = "")
sumn = 0
m = n
for a in range(1,n+1):
sumn = m*a
print("%d*%d=%2d"%(m,a,sumn),end = " ")
print("")
def hanshu1():
for b in range(9,0,-1):
hanshu(b)
hanshu1()
效果圖:
while i<=9:
j=1
while j<=i:
#%d爲佔位符 %2d表明佔兩個位置
print("%d*%d=%2d "%(i,j,i*j),end='')
j+=1
print()
i+=1
效果圖:
i = 1
while i<=9:
k=1
while k<=9-i:
print(' ',end='')#此處爲八個空格
k+=1
j=1
while j<=i:
print("%d*%d=%2d "%(i,j,i*j),end='')
j+=1
print()
i+=1
效果圖:
i = 9
while i>=1:
j=1
while j<=i:
print("%d*%d=%2d "%(i,j,i*j),end='')
j+=1
print()
i-=1
效果圖:
i = 9
while i>=1:
k=1
while k<=9-i:
print(' ',end='')#此處爲八個空格
k+=1
j=1
while j<=i:
print("%d*%d=%2d "%(i,j,i*j),end='')
j+=1
print()
i-=1
效果圖:
以上即是Python中利用for循環和while循環打印九九乘法表的一些小例子,但願這篇隨筆能給一起學習Python的朋友帶來一些幫助。
若有不足之處還請諸位多多包涵。
看完快去本身動手操做一下吧~~麼麼大。(大神請自動忽略這一句)