9.4.2: Function definition: Volume of a pyramid with modular functions

Question:

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))
Asked By: Jim Thorton

||

Answers:

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))
Answered By: HuLu ViCa

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))
Answered By: MK14

here is a simpler solution:

def pyramid_volume(length, width, height):
        return (length * width) * height/3
        
    length = float(input())
    width = float(input())
    height = float(input())
    print('Volume for', length, width, height, "is:", pyramid_volume(length, width, height)

I’m pretty new to this so I’m just hoping this might help.

Answered By: Askarr

The answer that worked for me is: (It is slightly modified from the other one to work with the most recent Zybooks version)

def base_area(base_length, base_width):
    return base_length * base_width

def pyramid_volume(base_length, base_width, pyramid_height):
    return (base_area(base_length, base_width) * pyramid_height)/3

length = float(input())
width = float(input())
height = float(input())
print('Volume for 4.5, 2.1, 3.0 is:', pyramid_volume(length, width, height))
Answered By: Kuruk

This is the solution that worked for me.

def calc_pyramid_volume(base_length, base_width, pyramid_height):

base_area = base_length * base_width

volume = (base_area * pyramid_height) / 3

return (base_length * base_width) * pyramid_height / 3
Answered By: user19918935
def calc_base_area(base_length, base_width):
   return base_length * base_width
def calc_pyramid_volume(base_length,base_width,pyramid_height):
    return (length*width) * height * 1/3
length = float(input())
width = float(input())
height = float(input())
print('Volume for', length, width, height, "is:", calc_pyramid_volume(length, width, height))

You do not need to include the calc_base_area function since you can just solve that in the calc_pyramid_volume function. Still should work as an answer in Zybooks though.

Answered By: NippieMan

Here’s the answer that worked for me when solving it.

I did:

def calc_pyramid_volume(base_length, base_width, pyramid_height):

area = length * width

volume = area * height * 1/3

return volume

and it worked perfectly for me.

Answered By: Ethan Young
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.