Python 計(jì)算三角形的面積

Document 對(duì)象參考手冊(cè) Python3 實(shí)例

以下實(shí)例為通過(guò)用戶(hù)輸入三角形三邊長(zhǎng)度,并計(jì)算三角形的面積:

# -*- coding: UTF-8 -*-

# Filename : test.py
# author by : www.o2fo.com


a = float(input('輸入三角形第一邊長(zhǎng): '))
b = float(input('輸入三角形第二邊長(zhǎng): '))
c = float(input('輸入三角形第三邊長(zhǎng): '))

# 計(jì)算半周長(zhǎng)
s = (a + b + c) / 2

# 計(jì)算面積
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('三角形面積為 %0.2f' %area)

執(zhí)行以上代碼輸出結(jié)果為:

$ python test.py 
輸入三角形第一邊長(zhǎng): 5
輸入三角形第二邊長(zhǎng): 6
輸入三角形第三邊長(zhǎng): 7
三角形面積為 14.70

Document 對(duì)象參考手冊(cè) Python3 實(shí)例