Python Calculate the Area of a Triangle
The following example calculates the area of a triangle by taking the lengths of its three sides as input from the user:
Example (Python 3.0+)
# -*- coding: UTF-8 -*-
# Filename : test.py
# author by : www.example.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:'))
# Calculate the semi-perimeter
s = (a + b + c) / 2
# Calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('Triangle area is %0.2f' %area)
Executing the above code produces the following output:
$ python test.py 输入三角形第一边长: 5 输入三角形第二边长: 6 输入三角形第三边长: 7 三角形面积为 14.70Other Extensions
Python3 Examples