Could someone simplify my code and get it to work

Your code kind of confuses me, It’s simple yet its so overly complicated at the same time than it needs to be.

I think it would be better if you can declare what you want to achieve and users here can provide a pseudo code or algorithm.