Rant: my loop ran 5 times before the crash and I finally get why
I was working through a Python exercise on Tuesday night and my loop kept printing the same line 5 times before it blew up with an IndexError. I sat there for 20 minutes confused, then realized my list only had 4 items and I was counting from 1 instead of 0. I had read the zero based indexing thing like 3 times but it never clicked until it broke my code in front of me. Does anyone have a tip for catching off by one stuff before running it, or do you just have to get burned a few times?
Did you ever do that thing in school where you count on your fingers and then run out and have to start over? That's what your story made me think of, ha. I remember learning to count fence posts and fence gaps as a kid, like if you have 4 posts there are only 3 gaps between them. Nobody told me that was basically off by one thinking, it just stuck in my head as a weird fact. Honestly that fence thing has saved me more times than any tutorial, because now when I write a range I picture the gaps and go wait, that's not right. So yeah, get burned a few times, but also find your own dumb picture for it.