用python写9*9乘法表
Python写出的九九乘法表(while循环)
while循环的的四种方向的九九乘法表
#方向一
i = 1
while i <= 9:
j = 1
while j <= i:
print("%d*%d=%-2d"%(i,j,i*j),end = ' ') # %d: 整数的占位符,'-2'代表靠左对齐,两个占位符
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
#下图为以上代码运行结果