Question

In: Computer Science

Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.


Modules Assignment python


  • 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.


Solutions

Expert Solution

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)


Related Solutions

REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). 1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input a. Invalid input should throw a message for the user. Example: Invalid input, please try again 2. Each options should ask users for relevant data....
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and...
Write a Python program that represents various geometric shapes. Each shape like rectangle, square, circle, and pentagon, has some common properties, ex. whether it is filled or not, the color of the shape, and number of sides. Some properties like the area of the shapes are determined differently, for example, area of the rectangle is length * widthand area of a circle is ??2. Create the base class and subclasses to represent the shapes. Create a separate test module where...
PYTHON: Write a script that imports the functions in the module below and uses all of...
PYTHON: Write a script that imports the functions in the module below and uses all of the functions. import math def _main():     print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")     f = -200     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     f = 125     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     c = 0     print("{:.2f} C is {:.2f} F".format(c, c2f(c)))     c = -200     print("{:.2f} C is {:.2f} F".format(c, c2f(c))) def f2c(temp):     #Converts Fahrenheit tempature to Celcius     if temp <...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
If the area of a square is 25 square feet, calculate the perimeter of the
If the area of a square is 25 square feet, calculate the perimeter of the square. Enter the value for the perimeter in the first blank and enter the units in the second. Enter units as ft. or square ft. Round your final answer to tenths if rounding is necessary ? 
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user...
rite a program that will calculate the perimeter and area of a rectangle. Prompt the user to input the length and width. Calculate the area as length * width. Calculate the perimeter as 2* length + 2* width. Display the area and perimeter. Please use a proper heading in a comment block (name, date, description of the program). Please use comments in your code. Run your program twice using different input. Copy the output and place it at the bottom...
Python 3 A program will be written that outputs various geometric shapes, rendered in characters, line-by-line...
Python 3 A program will be written that outputs various geometric shapes, rendered in characters, line-by-line using nested loops. Here is what you need to know: 1- Copy this and don't change the inputs in the provided code: # Get the size and drawing character from the user size = input('Please enter the size: ') # Validate the input, exit if bad if size.isdigit(): size = int(size) else: print("Exiting, you didn't enter a number:", size) exit(1) # Input the drawing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT