-
Notifications
You must be signed in to change notification settings - Fork 0
/
SphereCalc.py
68 lines (54 loc) · 2.18 KB
/
SphereCalc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import math
class Sphere:
"""
This class represents a sphere and provides methods to calculate
its properties like surface area, volume, and informative string descriptions.
Attributes:
radius (float): The radius of the sphere in centimeters.
"""
def __init__(self, radius):
"""
Initializes a Sphere object.
Args:
radius (float): The radius of the sphere in centimeters.
Raises:
TypeError: If the radius is not a number.
ValueError: If the radius is not a positive number.
"""
if not isinstance(radius, (int, float)):
raise TypeError("Radius must be a number.")
if radius <= 0:
raise ValueError("Radius must be a positive number.")
self.radius = radius
def surface_area(self):
"""
Calculates and returns the surface area of the sphere.
Formula: surface area = 4 * pi * radius^2
Returns:
float: The surface area of the sphere in square centimeters,
rounded to 5 decimal places.
"""
surface_area = 4 * math.pi * pow(self.radius,2)
return round(surface_area,5)
def volume(self):
"""
Calculates and returns the volume of the sphere.
Formula: volume = 4/3 * pi * radius^3
Returns:
float: The volume of the sphere in cubic centimeters,
rounded to 5 decimal places.
"""
volume = (4.0 / 3.0) * math.pi*pow(self.radius, 3)
return round(volume,5)
def get_description(self):
"""
Returns a tuple containing two formatted strings describing the sphere's
surface area and volume.
Returns:
tuple[str, str]: A tuple containing strings that describe the sphere's
surface area and volume in a user-friendly format.
"""
surface_area_info = "Sphere with radius {} cm has surface area {:.3f} cm2.".format(self.radius, round(self.surface_area(),3))
volume_info = "Sphere with radius {} cm has volume {:.3f} cm3.".format(self.radius, round(self.volume(),3))
return(surface_area_info, volume_info)
# help(Sphere)