Computer Science
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 appropriate functions.
Python Modules
2 Likes
Answer
# shape_calculator.py
import math
def area_square(side):
return side * side
def perimeter_square(side):
return 4 * side
def area_rectangle(length, width):
return length * width
def perimeter_rectangle(length, width):
return 2 * (length + width)
def area_triangle(base, height):
return 0.5 * base * height
def area_circle(radius):
return math.pi * radius * radius
def perimeter_circle(radius):
return 2 * math.pi * radius
def surface_area_cylinder(radius, height):
return 2 * math.pi * radius * (radius + height)
# shape.py
from shape_calculator import (area_square, perimeter_square, area_rectangle, perimeter_rectangle,
area_triangle, area_circle, perimeter_circle, surface_area_cylinder)
while True:
print("\nShape Calculator Menu:")
print("1. Square")
print("2. Rectangle")
print("3. Triangle")
print("4. Circle")
print("5. Cylinder")
print("6. Exit")
choice = input("Enter choice (1/2/3/4/5/6): ")
if choice == '1':
print("Square:")
side = float(input("Enter the side length: "))
print("Area:", area_square(side))
print("Perimeter:", perimeter_square(side))
elif choice == '2':
print("Rectangle:")
length = float(input("Enter the length: "))
width = float(input("Enter the width: "))
print("Area:", area_rectangle(length, width))
print("Perimeter:", perimeter_rectangle(length, width))
elif choice == '3':
print("Triangle:")
base = float(input("Enter the base length: "))
height = float(input("Enter the height: "))
print("Area:", area_triangle(base, height))
elif choice == '4':
print("Circle:")
radius = float(input("Enter the radius: "))
print("Area:", area_circle(radius))
print("Circumference:", perimeter_circle(radius))
elif choice == '5':
print("Cylinder:")
radius = float(input("Enter the radius: "))
height = float(input("Enter the height: "))
print("Surface Area:", surface_area_cylinder(radius, height))
elif choice == '6':
print("Exiting the program.")
break
else:
print("Invalid input. Please choose again.")
Output
Shape Calculator Menu:
1. Square
2. Rectangle
3. Triangle
4. Circle
5. Cylinder
6. Exit
Enter choice (1/2/3/4/5/6): 1
Square:
Enter the side length: 4
Area: 16.0
Perimeter: 16.0
Shape Calculator Menu:
1. Square
2. Rectangle
3. Triangle
4. Circle
5. Cylinder
6. Exit
Enter choice (1/2/3/4/5/6): 4
Circle:
Enter the radius: 12
Area: 452.3893421169302
Circumference: 75.39822368615503
Shape Calculator Menu:
1. Square
2. Rectangle
3. Triangle
4. Circle
5. Cylinder
6. Exit
Enter choice (1/2/3/4/5/6): 6
Exiting the program.
Answered By
3 Likes
Related Questions
Write a Python program to generate a random number between 0 and 9.
Define a function which accepts n as an argument and prints Fibonacci series till n.
Consider the temperature given below for the month of June in North India. Calculate the average temperature and median value. This temperature gets dipped with a variation of 20°C in the month of December. Write a Python program to calculate the changed median value and average temperature.
Location Temperature (in °C) Delhi 41 Shimla 32 Chandigarh 43 Rohtak 40 Srinagar 28 Sri Ganganagar 45 Create a menu-driven program using user-defined functions to implement a calculator that performs the following:
(a) Basic arithmetic operations (+, −, *, /)
(b) log10(x), sin(x), cos(x)