Basically, you can define your range function, including the upper value, and then use it in your code:
def range_with_stop(start, stop, step): return range(start, stop + step, step) In [201]: print(list(range_with_end(0, 100, 5))) [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]
EDIT
According to @ Reti43's answer, you can change this function to the following (because I think in this case, as @ Reti43 mentioned, you want to return the range (0, 40, 5)):
def range_with_stop(start, stop, step): if stop//step == 0: return(range(start, stop + step, step)) else: return(range(start, step * (stop//step), step)) In [265]: range_with_stop(start, 42, step) Out[265]: range(0, 40, 5) In [266]: range_with_stop(start, 45, step) Out[266]: range(0, 45, 5)