Computer Science
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
Python
Python Functions
7 Likes
Answer
def calculate_area(base, height):
area = (1/2) * base * height
return area
base_value = int(input("Enter the base value: "))
height_value = int(input("Enter the height value: "))
triangle_area = calculate_area(base_value, height_value)
print("Area of the triangle:", triangle_area)
Output
Enter the base value: 10
Enter the height value: 5
Area of the triangle: 25.0
Answered By
3 Likes
Related Questions
Assertion (A): To use positional arguments, the arguments need to be passed in the same order as their respective parameters in the function definition.
Reasoning (R): If three positional arguments are to be passed to the function, the first argument will be assigned to the first parameter, second argument to the second parameter and the third argument to the third parameter.
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true but R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
A program having multiple functions is considered better designed than a program without any functions. Why ?
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 * widthWrite 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:
* ** ***