python利用for..in遍历,while循环嵌套编译九九乘法表的几种模式

运用for....in...遍历的四种方向的九九乘法表:

左下角:

import sys
for i in range(1,10):
  for j in range(1,i+1):
    sys.stdout.write("%d*%d=%2d "%(i,j,i*j))
  print(" ")

运行结果如下:

 

左上角:

import sys
for i in range(9,0,-1):
  for j in range(1,i+1):
    sys.stdout.write("%d*%d=%2d "%(i,j,i*j))
  print("")

运行结果如下:

 

右下角:

import sys
for i in range(1,10):
  for n in range(1,10-i):
    sys.stdout.write(" ")
  for j in range(1,i+1):
    sys.stdout.write("%d*%d=%2d "%(i,j,i*j))
  print("")

运行结果如下:

右上角:

import sys
for i in range(9,0,-1):
  for n in range(1,10-i):
    sys.stdout.write(" ")
  for j in range(1,i+1):
    sys.stdout.write("%d*%d=%2d "%(i,j,i*j))
  print("")

运行结果如下:

运用while..循环嵌套的四种方向的九九乘法表:

左下角:

i = 1
while i<=9:
  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 = 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:
  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

运行结果如下:

 

posted @ 2017-11-12 21:21  崔恒跃  阅读(400)  评论(0编辑  收藏  举报