123k views
1 vote
Create a module (library) named MyTriangle that contains the following two functions: \# Returns true if the sum of any two sides is greater than the third side. def isValid(side1, side2, side3): \# Returns the area of the triangle. def area(side1, side2, side3): Create another program with a main function; the main function reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid. The formula for computing the area of a triangle is: s=( side 1+ side 2+ side 3)/2 area = √(s(s− side 1)(s− side 2)(s− side 3))

User Vuong Pham
by
7.8k points

1 Answer

2 votes

Final answer:

To create a module (library) named MyTriangle containing the required functions, you can define the functions within a Python file. The module should include the isValid() function, which checks if the sum of any two sides is greater than the third side, and the area() function, which calculates and returns the area of the triangle using the given formula. The main function of the program reads three sides for a triangle, checks if the input is valid using the isValid() function, and if valid, computes and displays the area of the triangle.

Step-by-step explanation:

To create a module (library) named MyTriangle that contains the required functions, you can define the functions within a Python file named MyTriangle.py. Here is an example implementation:

import math

def isValid(side1, side2, side3):
# Returns True if the sum of any two sides is greater than the third side
return side1 + side2 > side3 and side1 + side3 > side2 and side2 + side3 > side1


def area(side1, side2, side3):
# Returns the area of the triangle
s = (side1 + side2 + side3) / 2
return math.sqrt(s * (s - side1) * (s - side2) * (s - side3))


def main():
# Read three sides for a triangle
side1 = int(input('Enter side1: '))
side2 = int(input('Enter side2: '))
side3 = int(input('Enter side3: '))

# Check if input is valid
if isValid(side1, side2, side3):
# Compute and display the area
triangle_area = area(side1, side2, side3)
print('The area of the triangle is:', triangle_area)
else:
print('Invalid input. The sides do not form a valid triangle.')


# Call the main function
main()

This code defines a module named MyTriangle that contains the functions isValid() and area(). It also includes a main() function that reads three sides for a triangle, checks if the input is valid using the isValid() function, and computes the area using the area() function.

User Emre Efendi
by
6.5k points