Python Compute the Area of a Triangle
The following example takes the lengths of the three sides of a triangle from the user and computes the triangle's area:
Example (Python 3.0+)
-- coding: UTF-8 --
Filename : test.py
author by : www.runoob.com
a
=
float
(
input
(
'
Enter the first side length of the triangle:
'
)
)
b
=
float
(
input
(
'
Enter the second side length of the triangle:
'
)
)
c
=
float
(
input
(
'
Enter the third side length of the triangle:
'
)
)
Compute the semi-perimeter
s
=
(
a
b
c
)
/
2
Compute the area
area
=
(
s
(
s
a
)
(
s
b
)
(
s
c
)
)
**
0.5
(
'
The area of the triangle is %0.2f
'
%
area
)
The output of the code above is:
$ python test.py
输入三角形第一边长: 5
输入三角形第二边长: 6
输入三角形第三边长: 7
三角形面积为 14.70
Python3 Examples