Question 14 of 17advanced🔧 ApplyLong Answer5 marks

Write a program that contains user defined functions to calculate area, perimeter or surface area whichever is applicable for various shapes like square, rectangle, triangle, circle and cylinder. The user defined functions should accept the values for calculation as parameters and the calculated value should be returned. Import the module and use the appropriate functions.

Correct Answer

Step 1: Create the module file (shapes_module.py)

python

shapes_module.py

"""This module contains functions to calculate area, perimeter or surface area for various shapes."""

import math

Function for Square

def square(side): area = side * side perimeter = 4 * side return area, perimeter

Function for Rectangle

def rectangle(length, breadth): area = length * breadth perimeter = 2 * (length + breadth) return area, perimeter

Function for Triangle

def triangle(base, height, side1, side2, side3): area = 0.5 * base * height perimeter = side1 + side2 + side3 return area, perimeter

Function for Circle

def circle(radius): area = math.pi * radius * radius perimeter = 2 * math.pi * radius return area, perimeter

Function for Cylinder

def cylinder(radius, height): surface_area = 2 * math.pi * radius * (radius + height) return surface_area

Step 2: Import the module and use functions

python

Main program

import shapes_module as sm

Calculate for Square

s = float(input("Enter side of square: ")) area_sq, peri_sq = sm.square(s) print("Square Area:", area_sq, "Perimeter:", peri_sq)

Calculate for Rectangle

l = float(input("Enter length: ")) b = float(input("Enter breadth: ")) area_rec, peri_rec = sm.rectangle(l, b) print("Rectangle Area:", area_rec, "Perimeter:", peri_rec)

Calculate for Circle

r = float(input("Enter radius of circle: ")) area_cir, peri_cir = sm.circle(r) print("Circle Area:", area_cir, "Perimeter:", peri_cir)

Calculate for Cylinder

r = float(input("Enter radius of cylinder: ")) h = float(input("Enter height of cylinder: ")) sa_cyl = sm.cylinder(r, h) print("Cylinder Surface Area:", sa_cyl)

Exercise: SUGGESTED LAB. EXERCISES | Q: 7 | (Page 31)
For More Understanding

Explanation

This answer follows the textbook pattern shown in Program 7-16 for creating user-defined modules. The module contains user-defined functions that accept parameters and return calculated values using tuples (as demonstrated in Program 7-12). Each function is designed for a specific shape, calculating area and perimeter where applicable, or surface area for the cylinder. The main program imports the module and calls appropriate functions with user input values.

Solution Steps

  1. Step 1: Create a module file with docstring describing its purpose

  2. Step 2: Define functions for each shape accepting parameters

  3. Step 3: Calculate area, perimeter or surface area within functions

  4. Step 4: Return calculated values using tuples for multiple values

  5. Step 5: Save the module file

  6. Step 6: Import the module in main program

  7. Step 7: Call appropriate functions with parameters and display results