编写Python程序,打印九九乘法表,(矩形,左上三角,右上三角,下三角)
时间: 2024-10-10 11:11:03 浏览: 80
基础算法-python打印九九乘法表
在Python中,我们可以编写四个不同的函数分别打印九九乘法表的四种形式:矩形、左上三角形、右上三角形和下三角形。以下是对应的代码:
```python
def print_rectangle():
for i in range(1, 10):
for j in range(1, i + 1):
print(f"{j}x{i}={i*j}", end="\t")
print("\n")
def print_left_triangle():
for i in range(1, 10):
for j in range(1, i + 1):
print(f"{j}x{i}={i*j}", end=" ")
print()
def print_right_triangle():
for i in range(9, 0, -1):
for j in range(i):
print(f"{j+1}x{i}={i*(j+1)}", end=" ")
print()
def print_down_triangle():
for i in range(9, 0, -1):
for j in range(i):
print(f"{i*(j+1)}=x{j+1}{i}", end=" ")
print()
# 打印各形式的九九乘法表
print("矩形:")
print_rectangle()
print("\n左上三角:")
print_left_triangle()
print("\n右上三角:")
print_right_triangle()
print("\n下三角:")
print_down_triangle()
阅读全文