溫馨提示×

怎么用python輸出三角形面積和周長

小億
925
2023-12-07 22:33:21
欄目: 編程語言

要計算三角形的面積和周長,需要知道三角形的三條邊長。

以下是用Python計算三角形面積和周長的示例代碼:

import math

def calculate_area(a, b, c):
    # 使用海倫公式計算三角形的面積
    s = (a + b + c) / 2  # 計算半周長
    area = math.sqrt(s * (s - a) * (s - b) * (s - c))  # 計算面積
    return area

def calculate_perimeter(a, b, c):
    # 計算三角形的周長
    perimeter = a + b + c
    return perimeter

# 輸入三角形的三條邊長
a = float(input("請輸入三角形的第一條邊長:"))
b = float(input("請輸入三角形的第二條邊長:"))
c = float(input("請輸入三角形的第三條邊長:"))

# 調用函數計算三角形的面積和周長
area = calculate_area(a, b, c)
perimeter = calculate_perimeter(a, b, c)

# 輸出結果
print("三角形的面積為:", area)
print("三角形的周長為:", perimeter)

通過輸入三角形的三條邊長,程序會根據海倫公式計算出三角形的面積,并計算出三角形的周長。然后,將結果輸出到屏幕上。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女