Advertisement
Rementai

Range

Mar 23rd, 2023 (edited)
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. def my_range(*args):
  2.     number_of_args = len(args)
  3.     if number_of_args == 1:
  4.         start, stop, step = 0.0, args[0], 1.0
  5.     elif number_of_args == 2:
  6.         start, stop, step = args[0], args[1], 1.0
  7.     elif number_of_args == 3:
  8.         start, stop, step = args[0], args[1], args[2]
  9.     else:
  10.         raise ValueError("Function needs 1 to 3 arguments.")
  11.  
  12.     if step == 0:
  13.         raise ValueError("Step argument cannot be 0")
  14.  
  15.     result = []
  16.     while step > 0 and start < stop:
  17.         result.append(start)
  18.         start += step
  19.     while step < 0 and start < stop:
  20.         result.append(stop)
  21.         stop += step
  22.     return result
  23.  
  24.  
  25. def main():
  26.     try:
  27.         print(my_range(1.1, 2.2, 0.5))
  28.         print(my_range(1.1, 2.1, 0.5))
  29.         print(my_range(1.1, 2.2))
  30.         print(my_range(2.2))
  31.         print(my_range(1.1, 2.2, -0.5))
  32.         print(my_range())
  33.     except ValueError as e:
  34.         print("Error: ", e)
  35.  
  36. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement