0

I'm trying two variants of the python range function with the primary objective of making the function inclusive. However neither seems to be working

def main():
    for i in standard_range(1, 25, 1):
        print(i, end='')


def inclusive_range(*args):
    numargs = len(args)
    if numargs < 1:
        raise TypeError("Requires atleast one argument")
    elif numargs == 1:
        stop = args[0]
        start = 0
        step = 1
    elif numargs == 2:
        (start, stop) = args
        step = 1
    elif numargs == 3:
        (start, stop, step) = args
    else:
        raise TypeError("Cannot have more than three arguments")

    i = start
    while i <= stop:
        yield i
        i += step


def standard_range(start, stop, step):
    i = start
    while i <= stop:
        yield i
        i += step


if __name__ == " __main__ ":
    main()

Any help will be appreciated.

3
  • 3
    How are they not working? Provide expected output vs. current output. Commented Jul 17, 2014 at 6:05
  • It works for me. Which is the problem? Maybe the problem is defining main function before standard_range Commented Jul 17, 2014 at 6:07
  • No, the problem isn't the main() function's position. Since it isn't called until the other functions have been defined it won't cause any errors. But I agree it's confusing that the poster doesn't give expected vs. actual results. Commented Jul 17, 2014 at 6:11

1 Answer 1

1

If that is your exact code, main() won't run because you have spaces before and after __main__:

if __name__ == " __main__ ":
    main()

Remove the spaces:

if __name__ == "__main__":
    main()

standard_range should not print the stop value, so use < instead of <=:

while i < stop:

If I make those changes, and use this main:

def main():
    print(*standard_range(1,25,1))
    print(*inclusive_range(1,25,1))

The result is:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me, the space thing. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.