Q. Modify the above function to take a third parameter called shape type. Shape type should be either triangle or rectangle. Based on the shape, it should calculate the area.
Formula used: Rectangle Area = length *width
Answer =
def calculate_area(x , y , z) : if z == "rectangle" : print ("Area of Rectangle : - ", x * y) elif z == "triangle" : print ("Area of Triangle is : - ", 1 / 2 * x * y) else : print ("Error :( ") shape = input("Enter the Shape (rectangle OR triangle) :- ") base_or_length = float(input("Enter the 'Length or Base' : - ")) height_or_width = float (input ("Enter the 'Height or width' : - ")) calculate_area(base_or_length , height_or_width , shape)
Output :-
Enter the Shape (rectangle OR triangle) :- rectangle
Enter the 'Length or Base' : - 24
Enter the 'Height or width' : - 12
Area of Rectangle : - 288.0
>>>
Enter the 'Length or Base' : - 24
Enter the 'Height or width' : - 12
Area of Rectangle : - 288.0
>>>
Enter the Shape (rectangle OR triangle) :- triangle
Enter the 'Length or Base' : - 59
Enter the 'Height or width' : - 97
Area of Triangle is : - 2861.5
>>>
Just a simpler form
ReplyDeletedef Shape(Shp,a,b):
if Shp=="Triangle":
area=1/2*a*b
return area
elif Shp=="Rectangle":
area=a*b
return area
s=input("Enter the Shape: ",)
h_l=int(input("Enter the Height of the Triangle: "))
b_w=int(input("Enter the Base of the Triangle: "))
print("Area of ",s," is: ",Shape(s,h_l,b_w))
: )
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )