Learning from mistakes works because a mistake shows you exactly where your knowledge has a gap. You need three things: make the mistake small and early, read carefully what happened, and after you fix it, do the same task again on your own. This matters a lot when you learn to code, because you will get errors every day. People who avoid mistakes often repeat only what they already know and stay in the same place.
Why are mistakes so useful for learning?
When you read about a topic or watch an explanation, everything seems clear. When you close the book and try it yourself, the places where you get stuck appear. A mistake makes the difference between "I think I understand" and "I can do it" visible.
Two conditions make a mistake useful:
- You notice it quickly. A wrong answer on a test paper that comes back a week later teaches less than a wrong answer you see right away. This is why exercises with instant checks are valuable.
- You understand the cause. Knowing that something is "wrong" is not enough. You should be able to say, in your own words, what is wrong and why.
How do you read an error message in Python?
An error message is a set of directions. Look at this short program that should print the average of three days of sales at Corner Café:
sales = [125.50, 98.00, 143.25] total = sum(sales) print("Average: $" + total / len(sales))
It does not run. It stops with this error:
TypeError: can only concatenate str (not "float") to str
The red text looks scary at first, but the message answers three questions:
| Question | Answer in the message |
|---|---|
| What kind of error is it? | TypeError: the types do not work together in this operation |
| What exactly happened? | Python tried to join a string and a float with + |
| Where? | On the line shown in the traceback, the print line |
Once you understand the problem, the fix is short. Use an f-string to put the number inside the text:
sales = [125.50, 98.00, 143.25] total = sum(sales) print(f"Average: ${total / len(sales):.2f}")
Average: $122.25
You learned more than one line of code here. In Python, a string and a number do not join by themselves. A person who learns this once through an error will recognize it right away in another program.
What are the steps for learning from errors?
These four steps work for code, a math problem, or a budget table.
1. Stop and read. Read the whole error message, the wrong result, or the feedback. Many people see red text and start making random changes without reading.
2. Say your guess. Write one sentence: "I think this happened because...". Even if the guess is wrong, you see where your thinking went wrong.
3. Change one thing. If you change three things at once, you cannot know which one worked.
4. Do it again on your own. After the fix, close the solution and do the task again from the start. This is when the learning becomes lasting.
Should you keep a mistake log?
Yes. The most annoying part of making the same mistake a third time is that you already solved it before. A small mistake log prevents this. Three columns are enough:
| What happened? | Why did it happen? | Next time |
|---|---|---|
The average line gave a TypeError | I joined a string and a float with + | I put the number inside an f-string |
I went outside the list (IndexError) | I asked for position 3 in a list of three items | Positions start at 0, so the last item is 2 |
| I calculated a percent wrong | I divided the difference by the new value | For percent change, I divide by the old value |
Review the log once a week. This brings each topic back at planned intervals, and [spaced repetition for students](/blog/spaced-repetition-for-students) explains why that works.
How do you make it easier to make mistakes?
Much of the fear comes from a mistake seeming expensive. You can lower the price:
- Work in small pieces. Writing ten lines and running them needs less debugging than writing a hundred lines and running them.
- Run often. Check the result after every small step. If an error appears, the cause is in the last few lines you wrote.
- Practice where nobody watches. A mistake on your own computer or in an exercise window is visible only to you and breaks nothing.
- Break things on purpose. Take a working example, change one part, and predict what will happen. It is the fastest way to learn what error messages look like.
Common mistakes when learning from mistakes
- Running to the fix without reading the message. The last line of an error message often names the problem directly.
- Taking an error personally. "I cannot do this" is a conclusion. Your actual data is only "there is a type mismatch on this line".
- Copying the fix and moving on. Pasting a working answer closes the problem, but the same error returns next time.
- Practicing only easy questions. If you never make mistakes, you are probably repeating what you already know.
What to read next
- Read errors in a systematic way in the [error types and traceback](/learn/python/error-types-traceback) lesson.
- Learn to find the cause of a bug in the [debugging](/learn/python/debugging) lesson.
- See the errors beginners meet most often in [common Python errors](/blog/common-python-errors).
- Set a steady pace with [how to learn a new skill in 30 days](/blog/first-30-days-new-skill).
- #learning from mistakes
- #learning to code
- #debugging
- #error messages