#!/usr/bin/env python # @Time : 2018/3/15 17:13 # @Author : KiritoLiu # @Contact : kiritoliuyhsky@gmail.com # @Site : # @File : 乘法表for.py # @Software: PyCharm #靠左的九九乘法表 for j in range(1, 10): for i in range(1, j+1): print("{}*{}={:<2}".format(i, j, i*j), end=" ") print() print("="*62) for j in range(9, 0, -1): for i in range(1, j+1): print("{}*{}={:<2}".format(i, j, i*j), end=" ") print() print("="*62) #靠右的九九乘法表 for j in range(1, 10): print(" " * (9 - j), end="") for i in range(j, 0, -1): print("{}*{}={:<2}".format(i, j, i*j), end=" ") print() print("="*62) for j in range(9, 0, -1): print(" " * (9 - j), end="") for i in range(j, 0, -1): print("{}*{}={:<2}".format(i, j, i*j), end=" ") print() print("="*62) #剧中的九九乘法表 #正三角 for j in range(1, 10): print(" "* int((7 * (9 - j))/2), end="") for i in range(j, 0, -1): print("{}*{}={:<2}".format(i, j, i*j), end=" ") print() print("="*62) #倒三角 for j in range(9, 0, -1): print(" " * int((7 * (9 - j))/2), end="") for i in range(j, 0, -1): print("{}*{}={:<2}".format(i, j, i*j), end=" ") print()
while循环输出九九乘法表
#!/usr/bin/env python # @Time : 2018/3/15 17:46 # @Author : KiritoLiu # @Contact : kiritoliuyhsky@gmail.com # @Site : # @File : 乘法表while.py # @Software: PyCharm #靠左的九九乘法表 j = 1 while j <= 9: i = 1 while i < j+1: print("{}*{}={:<2}".format(i, j, i*j), end=" ") i += 1 print() j += 1 print("="*62) j = 9 while j >= 1: i = 1 while i< j+1: print("{}*{}={:<2}".format(i, j, i*j), end=" ") i += 1 print() j -= 1 print("=" * 62) #靠右的九九乘法表 j = 1 while j <= 9: print(" " * (9 - j), end="") i = j while i > 0: print("{}*{}={:<2}".format(i, j, i*j), end=" ") i -= 1 print() j += 1 print("=" * 62) j = 9 while j >= 1: print(" " * (9 - j), end="") i = j while i > 0: print("{}*{}={:<2}".format(i, j, i*j), end=" ") i -= 1 print() j -= 1 print("=" * 62) #剧中的九九乘法表 #正三角 j = 1 while j <= 9: print(" " * int((7 * (9 - j))/2), end="") i = j while i > 0: print("{}*{}={:<2}".format(i, j, i*j), end=" ") i -= 1 print() j += 1 print("=" * 62) #倒三角 j = 9 while j >= 1: print(" " * int((7 * (9 - j))/2), end="") i = j while i > 0: print("{}*{}={:<2}".format(i, j, i*j), end=" ") i -= 1 print() j -= 1
一句话输出乘法表
#这是我在网上看到的,Python真神奇啊 print('\n'.join([' '.join(['%s*%s=%-2s' % (y, x, x*y) for y in range(1, x+1)]) for x in range(1, 10)]))