python编程:从键盘输入 3 个数作为三角形的边长,根据其数值,判断能否构成等边三角形、等腰三角形或直角三角形。
时间: 2024-06-14 19:04:19 浏览: 24
以下是判断三角形类型并计算周长和面积的Python程序:
```python
import math
def is_triangle(a, b, c):
if a + b > c and b + c > a and c + a > b:
return True
else:
return False
def is_equ(a, b, c):
if is_triangle(a, b, c):
if a == b and b == c and c == a:
return True
else:
return False
else:
return -1
def is_iso(a, b, c):
if is_triangle(a, b, c):
if a == b or b == c or c == a:
return True
else:
return False
else:
return -1
def is_right(a, b, c):
if is_triangle(a, b, c):
if a**2 + b**2 == c**2 or b**2 + c**2 == a**2 or c**2 + a**2 == b**2:
return True
else:
return False
else:
return -1
def calculate_perimeter(a, b, c):
return a + b + c
def calculate_area(a, b, c):
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
return area
a = float(input("请输入第一个边长:"))
b = float(input("请输入第二个边长:"))
c = float(input("请输入第三个边长:"))
if is_equ(a, b, c):
print("构成等边三角形")
elif is_iso(a, b, c):
print("构成等腰三角形")
elif is_right(a, b, c):
print("构成直角三角形")
elif is_triangle(a, b, c):
print("构成一般三角形")
else:
print("不能构成三角形")
perimeter = calculate_perimeter(a, b, c)
area = calculate_area(a, b, c)
print("周长为:", perimeter)
print("面积为:", area)
```
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)