Python循环(包括while&for)打印九九乘法表的各种实例代码

2018-12-1813:31:37后端程序开发Comments3,104 views字数 1316阅读模式
利用Python循环(包括while&for)各种打印九九乘法表的实例。菜鸟小编觉得挺不错的,现在就分享给大家,也给大家做个参考。

一.for循环打印九九乘法表文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

#注意:由于缩进在浏览器不好控制,请大家见谅,后续会有图片传入。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

1.1 左下角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

  for i in range(1,10): 

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

        print('%d*%d=%2d\t'%(j,i,i*j),end='')
    print()

效果图:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

1.2 右下角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

for i in range(1,10):
   for k in range(i+1,10):
     print(end='   ') #此处为返回八个空格,请注意
   for j in range(1,i+1):
     print('%d*%d=%2d\t' % (j, i, i * j), end='')
   print()

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

1.3 左上角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

  for i in range(9,0,-1):
   for j in range(1,i+1):
     print('%d*%d=%2d\t' % (j, i, i * j), end='')
   print() #此处的目的主要是利用print特性换行

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

1.4 右上角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

  for i in range(1,10):
    for k in range(1,i):
     print(end='   ') #此处返回八个空格
   for j in range(9,i-1,-1):
      print('%d*%d=%2d\t' % (i, j, i * j), end='')
   print()

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

二.while循环打印乘法表(四种方法)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

2.1 左下角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

  i = 1
  while i <=9:
    j = 1
    while j <= i:
      print('%d*%d=%2d\t'%(i,j,i*j),end='')
      j+=1
   print()
   i +=1

效果图:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

2.2 右下角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

  i = 1
  while i <= 9:
    k = 9
    while k >i:
      print(end='    ')  #此处为八个空格
      k -= 1
      j = 1
    while j <= i:
      print('%d*%d=%2d\t'%(j,i,i*j),end='')
      j +=1
    print()
    i +=1

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

2.3 左上角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

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

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

2.4 右上角文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

  i = 9
  while i >= 1:
    k = 9
    while k > i:
      print(end='    ') #此处为八个空格
      k -= 1
    j = 1
    while j <=i:
      print('%d*%d=%2d\t'%(j,i,i*j),end='')
      j +=1

    print()
    i -= 1

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

后续:此处截图为当时源码,仅供参考文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

Python循环(包括while&for)打印九九乘法表的各种实例代码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

以上这篇利用Python循环(包括while&for)各种打印九九乘法表的实例就是菜鸟学院小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持菜鸟学院。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/8718.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/8718.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定