-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.py
More file actions
72 lines (56 loc) · 1.89 KB
/
cube.py
File metadata and controls
72 lines (56 loc) · 1.89 KB
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
69
70
71
72
"""
Cube module - Represents a 3D cube shape.
Provides:
- Side length validation
- Volume, surface area, and total edge length calculations
"""
from utils import validate_number
class Cube:
"""Represents a cube defined by the length of one side."""
def __init__(self, side: float):
"""Initialize a new Cube instance."""
self.side = side
@property
def side(self) -> float:
"""Get the side length of the cube."""
return self._side
@side.setter
def side(self, value: float):
"""Set the side length with validation."""
if isinstance(value, bool):
raise TypeError("Side must not be a boolean value")
validate_number(value)
if value <= 0:
raise ValueError("Side must be larger than zero")
self._side = float(value)
"""Geometric properties"""
@property
def volume(self) -> float:
"""Return the volume of the cube (side³)."""
return self.side**3
@property
def surface_area(self) -> float:
"""Return the surface area of the cube (6 × side²)."""
return 6 * self.side**2
@property
def perimeter(self) -> float:
"""Return the total edge length (12 × side)."""
return 12 * self.side
"""Representations"""
def __repr__(self) -> str:
return f"Cube(side={self.side})"
def __str__(self) -> str:
return (
f"Cube with side={self.side:.2f}, "
f"volume={self.volume:.2f}, "
f"surface_area={self.surface_area:.2f}"
)
if __name__ == "__main__":
try:
s = float(input("Please enter the side length of the cube: "))
c = Cube(s)
print(f"Volume: {c.volume:.2f}")
print(f"Surface Area: {c.surface_area:.2f}")
print(f"Total Edge Length: {c.perimeter:.2f}")
except (ValueError, TypeError) as e:
print("Error:", e)