Using Python create a module that contains area calculation functions of different shapes (triangle, circle and rectangle). You need to utilize this module in your main program that prompts user to enter the shape type and depending on user input it should return the accurate area calculation results.
Next, you need to create functions within your main program that calculate and display the volume and surface area of a cylinder.
Note: Avoid duplication to gain maximum marks. Make use of functions written inside module where ever possible to ensure reusability and modularity.
Module "area.py"
import math
def calculateAreaTriangle(a,b,c):
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c))**0.5
return area
def calculateAreaCircle(r):
return math.pi*r*r
def calculateAreaRectangle(w,h):
return w*h
main.py
import area
import math
def calculateVolumeCylinder(h,r):
return math.pi * r * r * h
def calculateSurfaceAreaCylinder(h,r):
return ((2*math.pi*r) * h) + ((math.pi*r**2)*2)
ch=-1
while ch!=5:
print("1. Calculate the area of a triangle")
print("2. Calculate the area of a circle")
print("3. Calculate the area of a rectangle")
print("4. Calculate the area of the volume and surface area of a cylinder")
print("5. Exit")
ch=int(input("Your choice: "))
if ch==1:
a = float(input('\nEnter the first side: '))
b = float(input('Enter the second side: '))
c = float(input('Enter the third side: '))
a=area.calculateAreaTriangle(a,b,c)
print(f"\nThe area of the triangle: {a}\n")
elif ch==2:
r = float(input('\nEnter the radius of the circle: '))
a=area.calculateAreaCircle(r)
print(f"\nThe area of the circle: {a}\n")
elif ch==3:
w = float(input('\nEnter the height of the rectangle: '))
h = float(input('Enter the width of the rectangle: '))
a=area.calculateAreaRectangle(w,h)
print(f"\nThe area of the circle: {a}\n")
elif ch==4:
h = float(input('\nEnter the height of cylinder: '))
r = float(input('Enter the radius of cylinder: '))
v=calculateVolumeCylinder(h,r)
sa=calculateSurfaceAreaCylinder(h,r)
print(f"\nThe volume of the cylinder: {v}")
print(f"The surface area of the cylinder: {sa}\n")
Output
Comments
Leave a comment