Day 3: Five Bugs to Reverse a String

Day 3: Five Bugs to Reverse a String
Tonight I found out how many ways there are to get one loop wrong.
The goal was simple on paper: reverse a string manually, no [::-1], no reversed(), no shortcuts. It's the kind of exercise that looks trivial until you're the one writing it at midnight with not much brainpower left. I got through exactly one of the three string-manipulation exercises carried over from Week 1 — and it took five separate bugs to get there.
The bugs, in order
1. return inside the loop. My first draft returned after processing a single character, because return was indented inside the for loop instead of after it. The function exited on the first pass, every time.
2. Indexing into the wrong variable. I had a variable holding len(s) an integer and I tried to index into that instead of into s, the actual string. Numbers aren't indexable. Easy mix-up, confusing error message.
3. Off-by-one on the range. Once I fixed that, I hit IndexError: string index out of range. I'd started my backward loop at len(s) instead of len(s) - 1. Classic length-vs-last-valid-index trap a string of length 2 has valid indices 0 and 1, not 2.
4. Accumulator reset inside the loop. My results = "" line was living inside the loop, which meant it wiped itself back to empty every single iteration. The function only ever "remembered" the last character it saw.
5. Direction mismatch. Even after moving the accumulator outside the loop, I was looping backward through the string but prepending characters (char + results) which is the strategy for looping forward. Reversing a string by hand only works if your iteration direction and your accumulation direction agree: forward + prepend, or backward + append. Mixing them cancels out.
And even after all that a technically correct function — calling reverse_string("hi") on its own printed nothing when I ran the script. Turns out return sends a value back silently; you still need print() to actually see it. That one's the mirror image of yesterday's bug, where I wrote return print(x) and got None back. Same underlying confusion — return and print do different jobs — just showing up from opposite ends on back-to-back days.
What actually stuck
- Reversing by hand needs iteration direction and accumulation direction to match. There's no version where you mix forward-loop with append, or backward-loop with prepend, and get the right answer.
- Length and last index are off by one, always. If you're about to write
range(len(x), ...)for anything touching valid indices, stop and ask if you meantlen(x) - 1. - Where you initialize an accumulator matters as much as how you update it. Inside the loop resets it every pass; it has to live above the loop, once.
returnandprint()are not interchangeable, in either direction forgetting that cost me a bug yesterday and a bug today.
Five precise things had to line up for one small loop to work. That's a decent reminder that "simple" and "easy" aren't the same word.
Vowel counting and the palindrome check are still sitting on the Week 1 checklist. Tomorrow.