My advice would be to drop the code for now. Back up and make a pseudocode. Once you get the ideas down, turning that into code is much easier.

To start you off:

You need to test whether an...