In: Computer Science
(PYTHON) Write a program that prompts user to enter three sides of a triangle. The program should determine if the three sides can form a triangle. If the three sides can form a triangle, then determine the type of the triangle.
There are three types of triangles:
The program should have a function called triangle_type() that takes 3 parameters, the lengths of each side. The triangle_type() function should return Equilateral, Isosceles, or Scalene according to the descriptions above. Do not use global variables. Function parameters must be used.
Description:
3 sides can form a triangle only if sum of any two sides exceeds the other side.
Code:
def triangle_type(a, b, c):#determines triangle type
if(a==b):#1st and 2nd sides are equal
if(b==c):#2nd and 3rd sides are equal
return "Equilateral"
else:
return "Isosceles"
elif(b==c):#3rd and 2nd sides are equal
return "Isosceles"
elif(a==c):#1st and 3rd sides are equal
return "Isosceles"
return "Scalene"#if no side is equal
x = int(input("Enter side1: "))
y = int(input("Enter side2: "))
z = int(input("Enter side3: "))
if((x + y > z) and ( x + z > y) and (y + z > x)):#sum of 2
sides should be greater than 3rd side
print("Triangle is " + triangle_type(x, y, z))
else:
print("Given sides cannot form a triangle")
Image of Code:
Output: