Perhaps you have an issue with mixing of tabs and spaces for indentation. Check if your indentation is consistent.
For example, if some lines are indented with tabs, and some with spaces, then visually the indentation might look correct, but Python could interpret the indentation differently than the way it appears in your editor.
You can try running Python with the -t
option, which will give you a warning if there is a mix of tabs & spaces indentation. E.g.:
python -t myprogram.py