python 打印四种乘法表和菱形
四种乘法表指的是左上,左下,右上,右下乘法表,要求结果用format形式控制,不使用%符# 左上显示
print('-'*31 + '左上显示' + '-'*31)
for i in range(1,10):
for j in range(1,10):
if i<=j:
print('{}*{}={:<2d}'.format(i,j,i*j),end = '')
print()
# 左下显示
print('-'*31 + '左下显示' + '-'*31)
for i in range(1,10):
for j in range(1,10):
if j<=i:
print('{}*{}={:<2d}'.format(i,j,i*j),end = '')
print()
# 右上显示
print('-'*31 + '右上显示' + '-'*31)
for i in range(1,10):
print(' '*(i-1)*8,end='')
for j in range(1,10):
if i<=j:
print('{}*{}={:<2d}'.format(i,j,i*j),end = '')
print()
# 右下显示
print('-'*31 + '右下显示' + '-'*31)
for i in range(1,10):
print(' '*(9-i)*8,end='')
for j in range(1,10):
if j<=i:
print('{}*{}={:<2d}'.format(i,j,i*j),end = '')
print()
# 输出菱形
print('-'*31 + '输出菱形' + '-'*31)
def printx():
a = ' '*(5-i)
b = '*'*(2*(i+1)-1)
print(a,b)
for i in range(5):
printx()
for i in range(3,-1,-1):
printx()
页:
[1]