我们可以使用while循环或者for循环来打印九九乘法表以及三角形。
# 打印三角形
row = 1
while row <= 7:
j = 1 # 每一行要打印的字符数
while j <= row:
print('*', end=' ')
j += 1
pass
print()
row += 1
pass
row = 9
while row >= 1:
j = 1
while j <= row:
print('+', end=' ')
j += 1
pass
print()
row -= 1
pass
row = 1
while row <= 5:
j = 1
while j <= 5 - row or 9 >= j >= 5 + row:
print(' ', end='')
j += 1
pass
while 5 + row > j > 5-row:
print('+ ', end='')
j += 1
pass
print()
row += 1
pass
for row in range(1, 9):
for col in range(1, row):
print('+', end=' ')
pass
print()
pass
# 打印九九乘法表
for row in range(1, 10):
for col in range(1, 10):
if col <= row:
print("{}X{}={}".format(row, col, row*col), end=' ')
pass
else:
print()
break
pass
pass
pass
for row in range(1, 10):
for col in range(1, row+1):
print('{}X{}={}'.format(row, col, row*col), end =' ')
pass
print()
pass