It's completely normal for programmers to get stuck sometimes. It happens even to experienced developers, and especially when doing unfamiliar things. Learning to persist and get unstuck is part of the programming skillset.
Below is a list of approaches and techniques. They are based on many years of experience developing software, and usually help beginners get unstuck.
Beginners get stuck for one of several reasons:
Consider which problem you have now, then read the corresponding section.
At the risk of stating the obvious: make sure you've understood the problem completely before you start solving it.
Here are some questions you can ask yourself to check your understanding:
You can ask others for clarification or re-read the problem statement. If any terminology is unfamiliar, look it up.
If you get errors or wrong outputs when you run your program, your program has one or more bugs that you'll have to find and fix.
If running your code gives an error, read the error message closely several times. It can be hard to understand error messages when you're not used to it, but if you can understand the error you'll often get unstuck.
Note that error messages often contain line numbers. These tell you approximately which line causes the problem, though they can be off sometimes. Try to carefully read the offending line several times; the problem is often subtle.
If you don't understand the error, you can:
When running code on Python Principles, errors are turned into links. Try clicking the link to visit the corresponding blog post that explains the error and how to fix it.
If your program runs, but produces a wrong or unexpected output, something is wrong with either your implementation or the logic behind it.
Note carefully which input produces a wrong output. Mentally step through your code with this input. Think about what each line does. This way you will hopefully notice when things start to go awry, letting you understand the problem. You can read more about this debugging approach in the Debugging by Simulation blog post.
You're having syntax problems if you don't precisely what to type to make Python do as you want. You can often use examples to get unstuck. You can:
You can also review the lesson that taught you how to use the concept you need.
When a coding problem is easy and familiar, we can typically do it without much thought. When we run into a challenging problem, it's easy to default to the same approach. You end up just trying things, hoping that something will work.
But when that fails, it's best to step back and think. Ensure that your mental model is clear before proceeding.
Write down in plain English what you want your program to do before you write any code. This forces you to develop a clear approach to the problem. It also prevents you from getting bogged down in the details of syntax. This approach is referred to as writing pseudocode first.
If you're asked to write a function with inputs and outputs, it's often helpful to think of some concrete input and walk through what the output should be and why.
When you struggle with a problem, get stuck, and then spend some time doing other things, your brain continues to work on the problem in the background. This process is known in science as incubation. Research shows that sleep is particularly effective for incubation, but even taking a 10 minute break can sometimes get you unstuck.
If you try returning to the problem the next day, you'll often be surprised to find that everything is much clearer.
Sometimes the problem seems clear in your head. But if you try explaining it to someone else, out loud, you'll see that your mental model was muddy all along. There is something about making your thoughts explicit and formal through language that often works to get programmers unstuck. This is known as rubber duck debugging.
So grab a friend, a pet, or a rubber duck, or hop on a chatroom. Explain the problem you're having out loud or in text. You may find it helps surprisingly much. For chat rooms you can join our Discord or try the /r/learnpython subreddit.
Try reading our post on how to approach a coding exercise. It gives a more structured approach that you'll probably find helpful for difficult exercises.