葫芦金刚

导航

Python 使用for...in...和 while 循环 实现8种格式的 九九乘法表

#九九乘法表 for...in ..

#左下角
for i in range(1,10):
    for j in range(1,i+1):
        print(' %d×%d=%2d'%(j,i,i*j), end = '')
    print()
print('-------------------------------------------------------------------------')

#右下角
for i in range(1,10):
    for k in range(i,9):
        print(end='       ')
    for j in range(1,i+1):
        print("%2d×%d=%2d" %(j,i,i*j), end = '')

    print()
print('-------------------------------------------------------------------------')

#左上角
for i in range(9,0,-1):
    for j in range(1,i+1):
        print("%2d×%d=%2d" %(j,i,i*j), end = '')
    print()
print('-------------------------------------------------------------------------')

#右上角
for i in range(1,10):
    for k in range(1,i):
        print(end ='       ')
    for j in range(9,i-1,-1):

        print("%2d×%d=%2d" %(j,i,i*j), end = '')
    print()


#九九乘法表 (while 循环)

#左下角
i = 1
while i <= 9:
    j = 1
    while j <= i:
        print(' %d×%d=%2d'%(i,j,i*j),end = '')
        j += 1
    i += 1
    print()
print('-------------------------------------------------------------------------')

#右下角
i = 1
while i <=9:
    k = 9
    while k > i:
        print(end='       ')
        k -= 1
    j = 1
    while j <= i:
        print(' %d×%d=%2d'%(j,i,i*j),end = '')
        j += 1
    i += 1
    print()
print('-------------------------------------------------------------------------')



#左上角
i = 9
while i >= 1:
    j = 1
    while j <= i:
        print(' %d×%d=%2d'%(j,i,i*j),end = '')
        j +=1
    i -=1
    print()
print('-------------------------------------------------------------------------')

#右上角

i = 9
while i >=1:
    k = 9
    while k > i:
        print(end='       ')
        k -= 1
    j = 1
    while j <= i:
        print(' %d×%d=%2d'%(j,i,i*j),end = '')
        j += 1
    i -= 1
    print()
print('-------------------------------------------------------------------------')

  

posted on 2017-11-08 22:08  葫芦金刚  阅读(1547)  评论(0编辑  收藏  举报