Computer Science
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(base, height, shape_type):
if shape_type == "triangle":
area = (1/2) * base * height
elif shape_type == "rectangle":
area = base * height
else:
area = None
print("Invalid shape type. Please specify either 'triangle' or 'rectangle'.")
return area
shape_type = input("Enter the shape type, triangle or rectangle: ")
base_value = int(input("Enter the base value: "))
height_value = int(input("Enter the height value: "))
area = calculate_area(base_value, height_value, shape_type)
print("Area of the", shape_type, "is ", area)
Output
Enter the shape type, triangle or rectangle: triangle
Enter the base value: 10
Enter the height value: 5
Area of the triangle is 25.0
Enter the shape type, triangle or rectangle: rectangle
Enter the base value: 8
Enter the height value: 9
Area of the rectangle is 72
Related Questions
A program having multiple functions is considered better designed than a program without any functions. Why ?
Write a function called calculate_area() that takes base and height as input arguments and returns area of a triangle as an output. The formula used is: Triangle Area = 1⁄2 * base * height
Write a function called print_pattern() that takes integer number as argument and prints the following pattern if the input number is 3.
* ** ***
If input is 4, then it should print:
* ** ***
What is the utility of : (i) default arguments, (ii) keyword arguments ?