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))
是相关代码。每当我打电话时,说:
j = crange.ChangeableRange(4)
list(j)
我得到错误:
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
为什么?我该如何解决?