Latest web development tutorials

Python calculate the area of ​​a triangle

Document Object Reference Examples Python3

The following examples are sides of a triangle by the length of the user input, and calculates the area of ​​the triangle:

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

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


a = float(input('输入三角形第一边长: '))
b = float(input('输入三角形第二边长: '))
c = float(input('输入三角形第三边长: '))

# 计算半周长
s = (a + b + c) / 2

# 计算面积
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('三角形面积为 %0.2f' %area)

Execute the above code output results:

$ python test.py 
输入三角形第一边长: 5
输入三角形第二边长: 6
输入三角形第三边长: 7
三角形面积为 14.70

Document Object Reference Examples Python3