Question

In: Computer Science

Calculate the Area and Perimeter for shapes using functions in Python. Your goal is to match...

Calculate the Area and Perimeter for shapes using functions in Python.

Your goal is to match the sample output below:

Welcome to my area and perimeter calculator
======================================================
Circle   : area = 39.82, perimeter = 22.37
Square   : area = 85.19, perimeter = 36.92
Rectangle: area = 41.24, perimeter = 34.24
Triangle : area = 21.16

Here is the code to use for your program (Note you may have to type this code into Trinket directly)

import math

# Add your code here

# TODO -> Add welcome function here

# TODO -> Add circle area function here

# TODO -> Add circle perimeter function here

# TODO -> Add square area function here

# TODO -> Add Square perimeter function here

# TODO -> Add rectangle area function here

# TODO -> Add rectangle perimeter function here

# TODO -> Add triangle area function here

# =====================================================================

# Main Code - DO NOT EDIT ANYTHING BELOW.  Add your functions above

displayWelcome()


radius = 3.56

area = calcAreaCircle(radius)

perimeter = calcPerimeterCircle(radius)

print('Circle   : area = {0:.2f}, perimeter = {1:.2f}'.format(area, perimeter))


side = 9.23

area = calcAreaSquare(side)

perimeter = calcPerimeterSquare(side)

print('Square   : area = {0:.2f}, perimeter = {1:.2f}'.format(area, perimeter))


width = 2.9

height = 14.22

area = calcAreaRect(width, height)

perimeter = calcPerimeterRect(width, height)

print('Rectangle: area = {0:.2f}, perimeter = {1:.2f}'.format(area, perimeter))


base = 7.97

height = 5.31

area = calcAreaTriangle(base, height)

print('Triangle : area = {0:.2f}'.format(area))

Solutions

Expert Solution

Python code:

import math

def displayWelcome():#function to display welcome message

    print("Welcome to my area and perimeter calculator")#prining welcome message

    print("=====================================================")

def calcAreaCircle(radius):#function to calculate area of circle

    return(3.14159265359*radius*radius)#return circle area

def calcPerimeterCircle(radius):#function to calculate perimeter of circle

    return(2*3.14159265359*radius)#return circle perimeter

def calcAreaSquare(side):#function to calculate area of square

    return(side*side)#return square area

def calcPerimeterSquare(side):#function to calculate perimeter of square

    return(4*side)#return square perimeter

def calcAreaRect(width, height):#function to calculate area of rectangle

    return(width*height)#return rectangle area

def calcPerimeterRect(width, height):#function to calculate perimeter of rectangle

    return(2*(width+height))#return rectangle perimeter

def calcAreaTriangle(base, height):#function to calculate area of triangle

    return(0.5*base*height)#return triangle area

displayWelcome()

radius = 3.56

area = calcAreaCircle(radius)

perimeter = calcPerimeterCircle(radius)

print('Circle   : area = {0:.2f}, perimeter = {1:.2f}'.format(area, perimeter))

side = 9.23

area = calcAreaSquare(side)

perimeter = calcPerimeterSquare(side)

print('Square   : area = {0:.2f}, perimeter = {1:.2f}'.format(area, perimeter))

width = 2.9

height = 14.22

area = calcAreaRect(width, height)

perimeter = calcPerimeterRect(width, height)

print('Rectangle: area = {0:.2f}, perimeter = {1:.2f}'.format(area, perimeter))

base = 7.97

height = 5.31

area = calcAreaTriangle(base, height)

print('Triangle : area = {0:.2f}'.format(area))

Screenshot of code:

Output screenshot:


Related Solutions

Write a Python module that contains functions to calculate the perimeter and area of at least 4 different geometric shapes.
Modules Assignment pythonWrite 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.
Circle Object Python program Compute the area and perimeter of a circle using Python classes. take...
Circle Object Python program Compute the area and perimeter of a circle using Python classes. take the radius from the user and find the area and perimeter of a circle please use comments to explain so i better understand
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...
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...
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 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...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
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,...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the table below. Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents. The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as follows: 0 –...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT