-
Notifications
You must be signed in to change notification settings - Fork 0
/
SquareCalc.py
66 lines (53 loc) · 2.21 KB
/
SquareCalc.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
class Square:
"""
This class represents a square and provides methods to calculate
its properties like perimeter, area, and informative string descriptions.
Attributes:
side_length (float): The side length of the square in centimeters.
"""
def __init__(self, side_length):
"""
Initializes a Square object.
Args:
side_length (float): The side length of the square in centimeters.
Raises:
TypeError: If the side_length is not a number.
ValueError: If the side_length is not a positive number.
"""
if not isinstance(side_length, (int, float)):
raise TypeError("Side length must be a number.")
if side_length <= 0:
raise ValueError("Side length must be a positive number.")
self.side_length = side_length
def circumference(self):
"""
Calculates and returns the circumference (perimeter) of the square.
Formula: circumference = 4 * side_length
Returns:
float: The circumference (perimeter) of the square in centimeters,
rounded to 5 decimal places.
"""
circumference = 4 * self.side_length
return round(circumference,5)
def area(self):
"""
Calculates and returns the area of the square.
Formula: area = side_length^2
Returns:
float: The area of the square in square centimeters,
rounded to 5 decimal places.
"""
area = pow(self.side_length, 2)
return round(area,5)
def get_description(self):
"""
Returns a tuple containing two formatted strings describing the square's
circumference and area.
Returns:
tuple[str, str]: A tuple containing strings that describe the square's circumference
and area in a user-friendly format.
"""
info_circumference = "Square with side length {} cm has circumference {} cm.".format(self.side_length, round(self.circumference(), 3))
info_area = "Square with side length {} cm has area {} cm2.".format(self.side_length, round(self.area(), 3))
return(info_circumference, info_area)
# help(Square)