30
My first Python loop printed the wrong number of times and I'm still confused
I wrote a simple 'for i in range(5):' loop last week, but it only printed four lines instead of five. Has anyone else run into this starting out? I think I'm missing something basic about how range() works.
3 comments
Log in to join the discussion
Log In3 Comments
michael_patel1d ago
Honestly, I used to get tripped up by that exact thing. I kept thinking range(5) meant it would count 1, 2, 3, 4, 5. Tbh it starts at zero, so it goes 0, 1, 2, 3, 4. That's still five steps, but if you were printing the number itself, you'd never see a 5. It feels backwards at first.
8
uma_webb2817d ago
Ugh, same thing happened to me!
4
miajenkins17d ago
Oh man, what if it's actually a weird system glitch that happens at a certain time of day? I had my thing fail twice last week, both around 3 PM. Checked my router logs and there was nothing wrong on my end.
7