Python 計(jì)算三角形的面積
以下實(shí)例為通過用戶輸入三角形三邊長度,并計(jì)算三角形的面積:
# -*- coding: UTF-8 -*- # Filename : test.py # author by : m.o2fo.com a = float(input('輸入三角形第一邊長: ')) b = float(input('輸入三角形第二邊長: ')) c = float(input('輸入三角形第三邊長: ')) # 計(jì)算半周長 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 輸入三角形第一邊長: 5 輸入三角形第二邊長: 6 輸入三角形第三邊長: 7 三角形面積為 14.70
更多建議: