Shape Area Calculation System: Python Programming Assignment

Verified

Added on  2021/04/16

|9
|1384
|74
Homework Assignment
AI Summary
This Python assignment focuses on developing a shape area calculation system. The program utilizes Python to calculate the areas of various geometric shapes including triangles, rectangles, circles, trapezoids, ellipses, and parallelograms. The solution includes test data and expected outputs to validate the functionality of the implemented methods. The code incorporates user input validation to handle incorrect inputs such as zero values for parameters. The program also uses functions to calculate the area for each shape and formats the output to two decimal places. The assignment demonstrates the use of conditional statements (if/elif/else), input/output operations, and mathematical functions to solve the problem. The program is designed to provide a user-friendly interface, prompting the user to select a shape and input the necessary dimensions. The program also includes error handling for invalid menu selections and parameter inputs. The code is well-documented, explaining the purpose of each code block.
Document Page
Running head: PROGRAMMING IN PYTHON
Programming in python
Name of the Student
Name of the University
Authors Note
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
PROGRAMMING IN PYTHON
1
Task 1
Document Page
PROGRAMMING IN PYTHON
2
Task 2
Test Data Table
Test
data
type
Test data
The
reason it
was
selected
The
output
expecte
d due to
the use
of the
test data
The screenshot of actual output when the test data is used
Normal
Shape
selection-
2
For
Length-
32.5
Width-
21.0
In order to
check that
the
implement
ed
functionali
ties for
menu
selection is
working
properly
Area
682.50
Square
meters
Normal
Selection
input-4
Radius
od of the
circle=4.
888
In order to
check the
functionali
ty of area
calculation
method of
circle,
when input
is provided
in floating
point
values
with three
decimal
points.
Area
75.06
square
centimet
res
Normal Selection
input=5
Semi
major
axes=
7.22
Semi-
minor
axes=
5.44
In order to
check if
the
program
returns 2
decimals
pace result
for the
input of
two
lengths
Document Page
PROGRAMMING IN PYTHON
3
Test Data Table
Test
data
type
Test data
The
reason it
was
selected
The
output
expecte
d due to
the use
of the
test data
The screenshot of actual output when the test data is used
that are
with two
decimal
places.
Abnor
mal
Menu
Selection
input-8
In order to
check if
the
program
prompts
proper
error
message
when an
invalid
menu item
is selected
by the user
The
program
should
prompt
error
message.
Abnor
mal
Menu
Selection
input -8
and
radius of
the circle
is
provided
as r=0
In order
check is
the
program
prompts
the error
when a
value of an
parameter
is provided
as 0
The
Program
of
prompts
an error
message
that the
user had
entered a
invalid
input for
the
radius
value to
calculate
the
circle.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
PROGRAMMING IN PYTHON
4
Test Data Table
Test
data
type
Test data
The
reason it
was
selected
The
output
expecte
d due to
the use
of the
test data
The screenshot of actual output when the test data is used
Task 3
from math import *
##importing necessary packages and modules
def Triangle(h, b):
#defining functions for calculating area
area1 = ((h * b) / 2)
return area1
def rectangl(l, w):
area2 = (l * w)
return area2
def circle(r):
Document Page
PROGRAMMING IN PYTHON
5
area3 = pi * r * r
return area3
def trapezoid(b, h):
area4 = b * h
return area4
def ellipse(a, b):
area5 = pi * a * b
return area5
def parallelogram(b, h):
area6 = b * h
return area6
def greeting():
##Function to greet the user after the completion of the area of the shape.
print('Thanks for using Shape Area Calculation System \n')
print('Good Bye')
##Code to format the output as required for the assignment
print('------------------------------------------------')
print('Welcome to Shape Area Calculation System(SACS)')
print('------------------------------------------------')
print('1.Triangle \t 2.Rectangle \t 3.Trapezoid \n')
print('4.Circle \t 5.Ellipse \t 6.Parallelogram \n')
# print('Please select the shape you would like to you would like to calculate area for (1-6):')
user_input = input('Please select the shape you would like to calculate area for (1-6):')
##Asking for Input from the user to select the shape to calculate the area
# userinput== int(input('Enter Your choice:'))
if (user_input == '1'):
print('Thank you!')
print('The shape you selected is Triangle')
bse = float(input('enter the base of the Triangle (in centimeters) \t'))
Document Page
PROGRAMMING IN PYTHON
6
hegt = float(input('enter height of the Triangle(in centimeters) \t'))
if(bse==0 or hegt==0):
###Checking whether any output is provided as 0.
print('Value Error!!!! You have entered an invalid length for one of the parameters ')
else:
area = Triangle(bse, hegt)
print('Area of the Triangle: {:.2f} Square centimeters \t'.format(area))
###used the {:.2f} to format the output to two decimal places.
greeting()
elif (user_input == '2'):
print('Thank you!')
print('The shape you selected is a Rectangle')
base = float(input('Please input the length (in centimeter) \t'))
height = float(input('Please input the width (in centimeters) \t'))
if (base == 0 or height == 0):
print('Value Error!!!! You have entered an invalid length for one of the parameters ')
else:
ars = rectangl(base, height)
print('area of the Rectangle is : {:.2f} Square Centimeters \t'.format(ars))
greeting()
elif (user_input == '3'):
print('Thank you!')
print('The shape you selected is Trapezoid ')
bs = float(input('enter the base of the Trapezoid \t'))
hgt = float(input('Enter the height of the Trapezoid \t'))
if (bs== 0 or hgt == 0):
print('Value Error!!!! You have entered an invalid length for one of the parameters ')
else:
ars2 = trapezoid(bs, hgt)
print('The area of the trapezoid is : {:.2f} \t'.format(ars2))
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
PROGRAMMING IN PYTHON
7
elif (user_input == '4'):
print('Thank you!')
print('The shape you selected is Circle \n ')
rad = float(input(' \n enter the radius of the circle \n'))
ars3 = circle(rad)
if (rad== 0 ):
##Validating the input of the user for the radius parameter.
print('Value Error!!!! You have entered an invalid radius length')
##Prompting the error message for the invalid input.
else:
print('The area of the Circle is: {:0.2f} \t'.format(ars3))
##formatting the result to two decimal point precession
elif (user_input == '5'):
print('Thank you!')
print('The shape you selected is Ellipse')
smajor = float(input('Enter the length of the semi-major axis \t'))
sminor = float(input('Enter the length of the semi minor axis \t'))
if (smajor == 0 or sminor == 0):
print('Value Error!!!! You have entered an invalid length for one of the parameters ')
else:
elliparea = ellipse(smajor, sminor)
print('The area of the trapezoid is: {:.2f} \t'.format(elliparea))
elif (user_input == '6'):
print('Thank you!')
print('The shape you selected is Parallelogram')
pbase = float(input('Enter the length of the base of the Parallelogram'))
pheight = float(input('Enter the height of the Parallelogram'))
if (pbase == 0 or pheight == 0):
print('Value Error!!!! You have entered an invalid length for one of the parameters ')
Document Page
PROGRAMMING IN PYTHON
8
else:
paralleoarea = parallelogram(pbase, pheight)
print('The area of the Parallelogram is : {:.2f} \t'.format(paralleoarea))
else:
print('\n Ooops Error!!!!!Enter correct Details to calculate area')
#For any invalid input provided by the user for
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]