Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyRangeIterator:
- def __init__(self, *args):
- self.number_of_args = len(args)
- if self.number_of_args == 1:
- self.start, self.stop, self.step = 0.0, args[0], 1.0
- elif self.number_of_args == 2:
- self.start, self.stop, self.step = args[0], args[1], 1.0
- elif self.number_of_args == 3:
- self.start, self.stop, self.step = args[0], args[1], args[2]
- else:
- raise ValueError("Function needs 1 to 3 arguments.")
- if self.step == 0:
- raise ValueError("Step argument cannot be 0")
- self.current = self.start
- def __iter__(self):
- return self
- def __next__(self):
- if (self.step > 0 and self.current >= self.stop) or (self.step < 0 and self.current <= self.stop):
- raise StopIteration
- else:
- result = self.current
- self.current += self.step
- return result
- def main():
- try:
- my_range_iterator = MyRangeIterator(1.1, 2.2, 0.5)
- for val in my_range_iterator:
- print(val, end=" ")
- print()
- my_range_iterator = MyRangeIterator(1.1, 2.1, 0.5)
- for val in my_range_iterator:
- print(val, end=" ")
- print()
- my_range_iterator = MyRangeIterator(1.1, 2.2)
- for val in my_range_iterator:
- print(val, end=" ")
- print()
- my_range_iterator = MyRangeIterator(2.2)
- for val in my_range_iterator:
- print(val, end=" ")
- print()
- my_range_iterator = MyRangeIterator(1.9, 0.9, -0.5)
- for val in my_range_iterator:
- print(val, end=" ")
- print()
- my_range_iterator = MyRangeIterator()
- for val in my_range_iterator:
- print(val, end=" ")
- print()
- except ValueError as e:
- print("Error:", e)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement