6 ms·
I never have off by one errors in Python, but have always experienced it in previous languages and their libraries. Guido chose very well. (The one exception
by rogerbinns 13y ago
I never have off by one errors in Python, but have always experienced it in previous languages and their libraries. Guido chose very well.
(The one exception is a function in the random module.)
- calpaterson 13y agoI frequently have off-by-ones with range. len(range(1, 100)) == 99
- anonymoushn 13y agoYes, it's 100-1. len(range(25,100)) == 75 == 100-25
- aidos 13y agoSimplify it: len(range(100)) == 100 Though - I've written a lot of python code and I never use range. The beauty of python is that all collections are iterable so you seldom need to specify ranges.
- jmmcd 13y agoStupid randint. I have to look it up every time. And I have to look up numpy's randint as well.
- FBT 13y agorandrange is the fixed randint, which works just as you'd expect. It's far preferable to randint, for the aforementioned reason, and ever since I started using it all those annoying off by one errors that randint kept giving me vanished. The one thing I still use randint for is rolling dice. randint(1,6) is the equivalent of a six-sided die, and is that bit more readable as such than the equivalent using randrange. But for any other purpose, randrange beats randint hands down. Off-by-one errors, be gone!