Yes, your mistake is in not keeping the idea of the program separate from its implementation.
When you have such a separate version, be it in your head, in comments or a separate text, then you can compare it to what your code does, either by reading the code or by running it with print statements in it.
You always need a version of the program that you fully understand, one that you can reason about, one that you can convince yourself of that it will lead to the desired result in all scenarios.
If you've only got the one version in code, then if you don't understand the code you're screwed, blindly hammering at it, hoping something will make it work. No guarantees of it being correct when it appears to be working, because there's no reasoning about why it would be correct.