-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircleCalc.py
66 lines (52 loc) · 2.13 KB
/
CircleCalc.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
import math
class Circle:
"""
This class represents a circle and provides methods to calculate
its properties like circumference, area, and informative string descriptions.
Attributes:
radius (float): The radius of the circle in centimeters.
"""
def __init__(self, radius):
"""
Initializes a Circle object.
Args:
radius (float): The radius of the circle 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 circumference(self):
"""
Calculates and returns the circumference of the circle.
Formula: circumference = 2 * pi * radius
Returns:
float: The circumference of the circle in centimeters, rounded to 5 decimal places.
"""
circumference = 2 * math.pi * self.radius
return round(circumference,5)
def area(self):
"""
Calculates and returns the area of the circle.
Formula: area = pi * radius^2
Returns:
float: The area of the circle in square centimeters, rounded to 5 decimal places.
"""
area = math.pi * pow(self.radius, 2)
return round(area,5)
def get_description(self):
"""
Returns a tuple containing two formatted strings describing the circle's
circumference and area.
Returns:
tuple[str, str]: A tuple containing strings that describe the circle's circumference
and area in a user-friendly format.
"""
info_circumference = "Circle with radius {} cm has circumference {:.3f} cm.".format(self.radius, round(self.circumference(), 3))
info_area = "Circle with radius {} cm has area {:.3f} cm2.".format(self.radius, round(self.area(), 3))
return(info_circumference, info_area)
# help(Circle)