[Python] Chapter 2-12 Output triangle area and perimeter (15 points)

topic

This question requires to write a program to calculate and output the area and perimeter according to the three sides a, b, c of the input triangle. Note: In a triangle, the sum of any two sides is greater than the third side. The formula for calculating the area of ​​a triangle: area=√s(s−a)(s−b)(s−c)​​, where s=(a+b+c)/2.

Input format:
The input is 3 positive integers, which respectively represent the 3 sides a, b, and c of the triangle.

area = area; perimeter = perimeter

output in format with two decimal places. Otherwise, output

These sides do not correspond to a valid triangle

Input sample 1:
5 5 3
Output sample 1:
area = 7.15; perimeter = 13.00
Input sample 2:
1 4 1
Output sample 2:
These sides do not correspond to a valid triangle

the code


import math

a,b,c = input().split()

a = float(a)
b = float(b)
c = float(c)
s = (a+b+c)/2

if (a+b>c and b+c>a and c+a>b):
    area = math.sqrt(s*(s-a)*(s-b)*(s-c))
    per = a+b+c
    print("area = %.2f; perimeter = %.2f"%(area,per))
else:
    print("These sides do not correspond to a valid triangle")

topic link

Portal

Guess you like

Origin blog.csdn.net/horizon08/article/details/108137486