Skip to content

Commit d1f3244

Browse files
committed
Merge pull request UWPCE-PythonCert#83 from imdavis/master
Session 07 Assignment: Circle Class
2 parents f91df56 + 9fcf807 commit d1f3244

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

Students/imdavis/session07/circle.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@ def radius(self):
1616

1717
@radius.setter
1818
def radius(self, radius):
19-
assert radius > 0, "radius must be nonzero and non-negative"
20-
self.__radius = radius
19+
if radius <= 0:
20+
raise ValueError("radius must be nonzero and non-negative")
21+
else:
22+
self.__radius = radius
2123

2224
@classmethod
2325
def from_diameter(cls, diameter):
24-
assert diameter > 0, "diameter must be nonzero and non-negative"
25-
return cls(diameter / 2.0)
26+
if diameter <= 0:
27+
raise ValueError("diameter must be nonzero and non-negative")
28+
else:
29+
return cls(diameter / 2.0)
2630

2731
@property
2832
def diameter(self):
2933
return self.radius * 2.0
3034

3135
@diameter.setter
3236
def diameter(self, value):
33-
assert value > 0, "diameter must be nonzero and non-negative"
34-
self.radius = value / 2.0
37+
if value <= 0:
38+
raise ValueError("diameter must be nonzero and non-negative")
39+
else:
40+
self.radius = value / 2.0
3541

3642
@property
3743
def area(self):

Students/imdavis/session07/test_circle.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,27 @@ def test_nonsense_radius():
123123
Make sure we can't create a Circle object with a negative or zero
124124
radius.
125125
"""
126-
with pytest.raises(AssertionError):
126+
with pytest.raises(ValueError):
127127
c = Circle(-1.234)
128-
with pytest.raises(AssertionError):
128+
with pytest.raises(ValueError):
129129
c = Circle(0)
130130
c = Circle(1.234)
131-
with pytest.raises(AssertionError):
131+
with pytest.raises(ValueError):
132132
c.radius = -1.234
133-
with pytest.raises(AssertionError):
133+
with pytest.raises(ValueError):
134134
c.radius = 0
135135

136136
def test_nonsense_diameter():
137137
"""
138138
Make sure we can't create a Circle object with a negative or zero
139139
diameter.
140140
"""
141-
with pytest.raises(AssertionError):
141+
with pytest.raises(ValueError):
142142
c = Circle.from_diameter(-2.468)
143-
with pytest.raises(AssertionError):
143+
with pytest.raises(ValueError):
144144
c = Circle.from_diameter(0)
145145
c = Circle.from_diameter(2.468)
146-
with pytest.raises(AssertionError):
146+
with pytest.raises(ValueError):
147147
c.diameter = -2.468
148-
with pytest.raises(AssertionError):
148+
with pytest.raises(ValueError):
149149
c.diameter = 0

0 commit comments

Comments
 (0)