I daresay that the original code that you are following is ... wrong. Therefore, add "print" statements as-needed to show you what it was attempting to do, then replace the whole thing wth sensible logic. Unfortunately in "old" code you will find a lot of crazy examples of this sort.