def __init__(self, maximum, start=0, step=1):
"""Sets the maximum, start, and step"""
try:
self.maximum = math.ceil(maximum)
self.start = math.ceil(start)
self.step = math.ceil(step)
except TypeError:
return "Error, attributes must be of type int or float"
def __iter__(self):
"""Iterates over the range"""
return iter(range(self.start, self.maximum, self.step))
is the relevant code. Whenever I call, say:
j = crange.ChangeableRange(4)
list(j)
I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "crange.py", line 16, in __iter__
return iter(range(self.start, self.maximum, self.step))
TypeError: 'str' object cannot be interpreted as an integer
Why? How do I fix this?
The range function expects integers for arguments. It looks like you’ve created a string for either start, maximum, or step with something like
self.maximum = int(math.ceil(maximum)).Also note that in Python 2, the math.ceil function returns a float value, so those would need to be converted to integers.
In Python 3, your code works fine: