In: Computer Science
Write a program that prompts for the lengths of the sides of a triangle and reports the three angles. Make sure you have a good introduction stating what the program does when it is run, and a helpful prompt for the user when asking for input:
i.e. Here is the generated output from a sample program:
This program computes the angles of a triangle given the lengths of the sides.
What is the length of side 1? <wait for user input>
What is the length of side 2? <wait for user input>
What is the length of side 3? <wait for user input>
angle 1 = <angle in degrees for side 1, side 2, and side 3>
angle 2 = <angle in degrees for side 2, side 3, and side 1>
angle 3 = <angle in degrees for side 3, side 1, and side 2>
Requirements
Your program must have at least 1 function with parameters, that when called with arguements returns a value.
Hints
Save your program as triangle_angles.py and attach it.
For the given triangle,
When three sides are given, angles can be calculated using cosine law.
In the output please take care that the angle 1 is the angle opposite to side 1, angle 2 is the angle opposite to side 2,
The code to do so is:
The sample input and output for this code is:
**The codes are well commented and easy to understand, if the answer helped you please upvote and if you have any doubts, please comment i will surely help. Please take care of the indentation while copying the code. Check from the screenshot provided. **
**Code: **
import math
def findAngles(s1, s2, s3):
#"angles" is the list to store the angles of the triangle
angles=[]
#Calculate the angles using the cosine law
#To get the angles in degrees, use math.degrees() function
a1 = math.degrees(math.acos((s2**2 + s3**2 -
s1**2)/(2*s2*s3)))
a2 = math.degrees(math.acos((s1**2 + s3**2 -
s2**2)/(2*s1*s3)))
a3 = math.degrees(math.acos((s1**2 + s2**2 - s3**2)/(2*s1*s2)))
#Storing the angles in the list "angles"
angles.append(a1)
angles.append(a2)
angles.append(a3)
return angles
print("This program is used to find the angles of a triangle
when its three sides are given")
print("So Enter the three sides of a triangle")
# Take the input data
s1 = float(input("Enter the first side: "))
s2 = float(input("Enter the second side: "))
s3 = float(input("Enter the third side: "))
# Function call to find and return angles in the form of a
list
angles = findAngles(s1, s2, s3)
print("Angles in degrees: ")
for x in range(len(angles)):
print(f"Angle{x+1} = ", angles[x])