Your logic is correct, you should probably remove that code and think about those if statements you put there.
Read the problem statement again.