I'm not sure where I'm off at, here is the ask. Define a function calc_pyramid_volume() with parameters base_length, base_width, and pyramid_height, that returns the volume of a pyramid with a rectangular base. calc_pyramid_volume() calls the given calc_base_area() function in the calculation.
Relevant geometry equations: Volume = base area x height x 1/3 (Watch out for integer division).
Sample output with inputs: 4.5 2.1 3.0 Volume for 4.5, 2.1, 3.0 is: 9.45
Naturally, my code is not working.
def calc_base_area(base_length, base_width):
return base_length * base_width
def calc_pyramid_volume(base_area, pyramid_heigth):
return calc_base_area * pyramid_heigth
length = float(input())
width = float(input())
height = float(input())
print('Volume for', length, width, height, "is:", calc_pyramid_volume(length, width, height))
CodePudding user response:
You must first calculate the area of the base:
def calc_base_area(base_length, base_width):
return base_length * base_width
def calc_pyramid_volume(base_area, pyramid_heigth):
return calc_base_area * pyramid_heigth
length = float(input())
width = float(input())
height = float(input())
base = calc_base_area(length, width)
print('Volume for', length, width, height, "is:", calc_pyramid_volume(base, height))
CodePudding user response:
Its not working because, inside the calc_pyramid_volume you've only referenced the function calc_base_area but not called it with the relative arguments, so all you have to do is to call it with the base_length and base_width arguments and return a third of the result, like this...
def calc_base_area(base_length, base_width):
return base_length * base_width
def calc_pyramid_volume(base_length, base_width, pyramid_height):
return (calc_base_area(base_length, base_width) * pyramid_height)/3
length = float(input())
width = float(input())
height = float(input())
print('Volume for', length, width, height, "is :", calc_pyramid_volume(length, width, height))
