三角形 python
时间: 2023-11-02 19:03:57 浏览: 97
三角形的python代码可能包括计算三角形面积的函数,以及打印出特定形状的三角形的函数。以下是一个简单的示例代码:
```python
# 计算三角形面积的函数
def calculate_triangle_area(base, height):
area = 0.5 * base * height
return area
# 打印出特定形状的三角形的函数
def print_triangle(shape, rows):
if shape == "正":
for i in range(rows):
for j in range(rows - i):
print(" ", end="")
for k in range(2 * i + 1):
print("*", end="")
print()
elif shape == "倒":
for i in range(rows):
for j in range(i):
print(" ", end="")
for k in range(2 * (rows - i) - 1):
print("*", end="")
print()
# 使用示例
base = 5
height = 3
area = calculate_triangle_area(base, height)
print("三角形面积:", area)
print("正三角形:")
print_triangle("正", rows)
print("倒三角形:")
print_triangle("倒", rows)
```
阅读全文