In: Computer Science
Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Write a separate Python program (in a separate file from you Python module) that imports your above module and uses several of its functions in your program. Make use of the input() and print() functions in this program file. Try to make your program as a real-world application.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
Note: Make sure you copy the code for each module separately (I have attached utility.py and main.py. Don’t change anything, and do not copy everything to a single file.)
#code
#utility.py (should be saved with that name only) import math #method to calculate area of a circle given radius def areaCircle(radius): return math.pi*radius*radius #method to calculate perimeter/circumference of a circle given radius def perimeterCircle(radius): return 2*math.pi*radius #method to calculate area of a square given side def areaSquare(side): return side*side #method to calculate perimeter of a square given side def perimeterSquare(side): return 4*side #method to calculate area of a rectangle given length and width def areaRectangle(length, width): return length*width #method to calculate perimeter of a rectangle given length and width def perimeterRectangle(length, width): return 2*(length+width) #method to calculate area of a triangle given base and height def areaTriangle(base, height): return 0.5*base*height #method to calculate perimeter of a triangle given three sides def perimeterTriangle(a,b,c): return a+b+c #end of utility.py file #main.py (contains the main module) #importing all modules from utility.py file from utility import * #method to print a menu containing all choices def menu(): print('\n1. Area of Circle') print('2. Area of Square') print('3. Area of Rectangle') print('4. Area of triangle') print('5. Perimeter of Circle') print('6. Perimeter of Square') print('7. Perimeter of Rectangle') print('8. Perimeter of triangle') print('9. Quit') #main method def main(): ch = 0 #looping until ch is 9 (to quit) while ch != 9: #printing menu, reading choice menu() ch = int(input("Your choice: ")) #handling choice if ch == 1: radius = float(input("Enter the radius of the circle: ")) print('Area is', areaCircle(radius)) elif ch == 2: side = float(input("Enter the side length of the square: ")) print('Area is', areaSquare(side)) elif ch == 3: length = float(input("Enter the length of the rectangle: ")) width = float(input("Enter the width of the rectangle: ")) print('Area is', areaRectangle(length, width)) elif ch == 4: base = float(input("Enter the base of the triangle: ")) height = float(input("Enter the height of the triangle: ")) print('Area is', areaTriangle(base, height)) elif ch == 5: radius = float(input("Enter the radius of the circle: ")) print('Perimeter is', perimeterCircle(radius)) elif ch == 6: side = float(input("Enter the side length of the square: ")) print('Perimeter is', perimeterSquare(side)) elif ch == 7: length = float(input("Enter the length of the rectangle: ")) width = float(input("Enter the width of the rectangle: ")) print('Perimeter is', perimeterRectangle(length, width)) elif ch == 8: a = float(input("Enter side1 of the triangle: ")) b = float(input("Enter side2 of the triangle: ")) c = float(input("Enter side3 of the triangle: ")) print('perimeter is', perimeterTriangle(a, b, c)) #invoking main() main() #end of main.py file
#output (partial)