97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
import math
|
|
|
|
|
|
def volume():
|
|
# Ask for the shape of the object
|
|
choice = input(
|
|
"Enter shape of object. 1 for Cylinder, 2 for Sphere, 3 for Cone, or 4 for Rectangular Box: ")
|
|
|
|
# Check if the input is numeric and within range
|
|
if not choice.isdigit():
|
|
print(
|
|
"Input not recognized. Please choose a shape using 1, 2, 3, or 4!")
|
|
return
|
|
else:
|
|
choice = int(choice)
|
|
|
|
if choice not in [1, 2, 3, 4]:
|
|
print(
|
|
"TInput not recognized. Please choose a shape using 1, 2, 3, or 4!")
|
|
return
|
|
|
|
# Cylinder
|
|
if choice == 1:
|
|
radius = input("Enter the cylinder radius: ")
|
|
height = input("Enter the cylinder height: ")
|
|
|
|
# Validate radius and height
|
|
# Remove any '.' from input to string to check if valid numeral
|
|
testRadius = radius.replace('.', '')
|
|
testHeight = height.replace('.', '')
|
|
if not testRadius.isdigit() or not testHeight.isdigit():
|
|
print("Please enter a valid number")
|
|
return
|
|
else:
|
|
radius = float(radius)
|
|
height = float(height)
|
|
|
|
volume = math.pi * radius ** 2 * height
|
|
print("Volume is: ", volume, "units")
|
|
|
|
elif choice == 2: # Sphere
|
|
radius = input("Enter radius of the sphere: ")
|
|
|
|
# Validate radius inputs
|
|
if not radius.replace('.', '').isdigit():
|
|
print("Please enter a valid number for radius.")
|
|
return
|
|
else:
|
|
radius = float(radius)
|
|
|
|
|
|
volume = (4 / 3) * math.pi * radius ** 3
|
|
print("Volume: ", volume, "units")
|
|
|
|
elif choice == 3: # Cone
|
|
radius = input("Enter radius of the cone: ")
|
|
height = input("Enter height of the cone: ")
|
|
|
|
# Validate radius and height
|
|
testRadius = radius.replace('.', '')
|
|
testHeight = height.replace('.', '')
|
|
if not testRadius.isdigit() or not testHeight.isdigit():
|
|
print("Please enter a valid number")
|
|
return
|
|
else:
|
|
radius = float(radius)
|
|
height = float(height)
|
|
|
|
volume = (math.pi * radius ** 2 * height) / 3
|
|
print("Volume: ", volume, "units")
|
|
|
|
elif choice == 4: # Rectangular Box
|
|
width = input("Enter the width of the rectangular box: ")
|
|
height = input("Enter the height of the rectangular box: ")
|
|
length = input("Enter the length of the rectangular box: ")
|
|
|
|
# Validate width, height, and length
|
|
testWidth = width.replace('.', '')
|
|
testHeight = height.replace('.', '')
|
|
testLength = length.replace('.','')
|
|
|
|
if not testWidth.isdigit() or not testHeight.isdigit() or not testLength.isdigit():
|
|
print("Please enter a valid number")
|
|
return
|
|
|
|
else:
|
|
width = float(width)
|
|
height = float(height)
|
|
length = float(length)
|
|
|
|
volume = width * height * length
|
|
print("Volume: ", volume, "units")
|
|
|
|
|
|
# Call the volume function
|
|
volume()
|