When your code isn’t working, it can be tempting to start haphazardly guessing and changing it. Generally, though, this does not move you towards a working solution. It helps to examine your entire problem-solving approach – are you being strategic? Do you know how to solve the problem yourself by hand? Do you have a plan for how to turn your solution into code? Then, develop the code a little bit at a time, testing after you implement each small piece of functionality. When you test, you should be sure you know what the correct answer is before you run the code.
If you get the wrong answer, it is time to debug. If you have a solid plan (and comments to guide you), you can be strategic in how you debug to more effectively and efficiently resolve the errors. And, articulating your process to others can sometimes help you recognize where you’ve gone astray or something you’ve missed; it will also give others a chance to understand where you are in the process and how to provide support!
After you get a piece of functionality working, then add another piece of functionality, test, debug, repeat, until your program is done!