forked from onyx44q/AI-Coursework-2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvolume.py
More file actions
22 lines (17 loc) · 778 Bytes
/
volume.py
File metadata and controls
22 lines (17 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import math
try:
radius_str = input("Enter the radius of the sphere: ")
# Convert the user's input from a string to a floating-point number
radius = float(radius_str)
# Check if the radius is a non-negative number
if radius < 0:
print("\nEnter a positive number.")
else:
# Calculate the volume of the sphere using the formula V = (4/3) * pi * r^3
# The exponential operator (**) is used to calculate the cube of the radius.
volume = (4/3) * math.pi * (radius ** 3)
print(f"\nThe volume of a sphere with radius is {volume}.")
except ValueError:
# If the user's input cannot be converted to a number,
# this message is printed.
print("\nEnter a valid number for the radius.")