Skip to content

Commit 0f5a9bd

Browse files
committed
Fix constructor type checking logic
1 parent 0235838 commit 0f5a9bd

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

chaos_game.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,36 @@
44

55
class ChaosGame:
66
def __init__(self, n: int = 3, r: float = 0.5):
7-
"""Need to make sure input is of right type, and 0 < r > 1, n > 2.
7+
"""Need to make sure input is of right type, and 0 < r < 1, n > 2.
88
This checks first, then sets the attributes after. sets to default if not possible. """
99
try:
1010
if not isinstance(n, int):
11-
raise TypeError("n should be an int > 2")
11+
raise TypeError("n should be an int")
1212
except TypeError as terr:
1313
print(terr.args)
1414
print("trying to convert")
1515
n = int(n)
16-
if n < -2:
17-
self.n = self.n * -1
18-
elif -2 <= n <= 2:
19-
print("using default n, 3")
20-
self.n = 3
16+
if n < -2:
17+
print("n need to be int > 2, converting to positive int")
18+
self.n = self.n * -1
19+
elif -2 <= n <= 2:
20+
print("using default n = 3")
21+
self.n = 3
22+
else:
23+
self.n = n
24+
2125
try:
22-
if not isinstance(n, float):
23-
raise TypeError("r should be an float 0 < r > 1")
26+
if not isinstance(r, float):
27+
raise TypeError("r should be a float 0 < r < 1")
2428
except TypeError as terr:
2529
print(terr.args)
2630
print("trying to convert")
2731
r = float(r)
28-
if not (0 < r > 1):
29-
print("using default, 0.5")
30-
self.r = 0.5
31-
else:
32-
self.r = r
32+
if not (0 < r < 1):
33+
print("using default = 0.5")
34+
self.r = 0.5
35+
else:
36+
self.r = r
37+
3338

3439
game = ChaosGame()

0 commit comments

Comments
 (0)