This Python program receives the dimensions of a box—length, width, and height—as input parameters.
It uses a method to calculate and display the volume of the box.
The formula for volume is:
Volume = Length × Width × Height
This exercise helps users become familiar with defining and using methods with parameters.
def calculate_volume(length: float, width: float, height: float) -> float:
return length * width * height
# Read dimensions from user
length = float(input("Enter box length: "))
width = float(input("Enter box width: "))
height = float(input("Enter box height: "))
volume = calculate_volume(length, width, height)
print(f"Box volume: {volume:.2f} cubic units")
Box volume: 24.00 cubic units
- The user enters the box’s length, width, and height
- These values are passed to a method that calculates the volume
- The result is displayed with two decimal places