Python Principles

Getting unstuck

It's completely normal for programmers to get stuck sometimes. It happens even to experienced developers, and quite frequently 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.

Determine the cause

Beginners get stuck for one of two reasons:

  • not knowing how to make Python do what you want
  • not knowing what to do to solve the problem

The first is a syntax problem. The second is a thinking problem.

Which of the two problems are you struggling with right now?

Syntax problems

You're having syntax problems if you don't precisely what to type to make Python do as you want.

Use examples

You can often use examples to figure out how to do something. You can:

  • review previous examples you've seen
  • combine bits of code from previous examples
  • base your solution on the reference sheet

Deal with errors

If you've written code and are getting an error, read the error message closely several times. It can be hard to understand errors, but if you can understand the error you'll often get unstuck.

If you don't understand the error, you can:

  • google the error
  • compare your code to previous examples you've seen
  • compare your code to the reference sheet

Thinking problems

When a software 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, we must step back and think. We must ensure that our mental model is clear before proceeding.

Understand the task

At risk of stating the obvious, if you're given an exercise and some words aren't clear, then review the terminology. It should be completely clear to you what you're trying to accomplish, even if you don't yet know how to get there.

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.

Devise a strategy

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.

Sleep on it

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 as incubation in science. Research shows that sleep is particularly effective for incubation.

If you try returning to the problem the next day, you'll often be surprised to find that everything is much clearer.

Think out loud

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.

Use a structured approach

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.

Get help

We're available to help you on the Discord chatroom. You can also usually get help from fellow coders on the /r/learnpython subreddit which we're not affiliated with.