DEV Community

Cover image for “Debugging 101: How to Read and Understand Python Error Messages”
Mithu_kr
Mithu_kr

Posted on

“Debugging 101: How to Read and Understand Python Error Messages”

We’ve all been there — you run your code confidently, only to see a red wall of error messages screaming back at you.

But here’s the secret 👉 those errors aren’t your enemies — they’re your teachers.

In this article, we’ll decode 5 of the most common errors, understand what they mean, why they happen, and how to fix them — so next time you debug, you do it like a pro 🔍


1️⃣ Undefined Variable Error

🧩 What It Means

Your code tries to use a variable before it has been defined.

💥 Example

print(name)  # 'name' is not defined
Enter fullscreen mode Exit fullscreen mode

💀 Error Message

NameError: name 'name' is not defined
Enter fullscreen mode Exit fullscreen mode

✅ Fix

name = "Rohan"
print(name)
Enter fullscreen mode Exit fullscreen mode

🧰 Debug Tip

Use print() to check variables:

print(locals())
Enter fullscreen mode Exit fullscreen mode

2️⃣ Syntax Error

🧩 What It Means

Your code breaks Python’s grammar rules — missing colons, wrong indentation, etc.

💥 Example

def greet()
    print("Hello")
Enter fullscreen mode Exit fullscreen mode

💀 Error Message

SyntaxError: expected ':'
Enter fullscreen mode Exit fullscreen mode

✅ Fix

def greet():
    print("Hello")
Enter fullscreen mode Exit fullscreen mode

🧰 Debug Tip

Use an IDE or linter (like flake8 or black) — it catches syntax issues early.


3️⃣ Type Error

🧩 What It Means

You’re trying to mix incompatible data types.

💥 Example

x = "5" + 3
Enter fullscreen mode Exit fullscreen mode

💀 Error Message

TypeError: can only concatenate str (not "int") to str
Enter fullscreen mode Exit fullscreen mode

✅ Fix

x = int("5") + 3
print(x)
Enter fullscreen mode Exit fullscreen mode

🧰 Debug Tip

Print the data type before operations:

print(type(x))
Enter fullscreen mode Exit fullscreen mode

4️⃣ Network Error

🧩 What It Means

Your program failed to connect to an external server or API.

💥 Example

import requests

response = requests.get("https://invalid-url-example.com")
print(response.text)
Enter fullscreen mode Exit fullscreen mode

💀 Error Message

requests.exceptions.ConnectionError: Failed to establish a new connection
Enter fullscreen mode Exit fullscreen mode

✅ Fix

try:
    response = requests.get("https://api.github.com/users/octocat")
    print(response.json())
except requests.exceptions.RequestException as e:
    print("Network Error:", e)
Enter fullscreen mode Exit fullscreen mode

🧰 Debug Tip

  • Check your internet or URL spelling
  • Use ping or curl to test manually
  • Add timeout=5 to avoid hanging requests

5️⃣ Infinite Loop Error

🧩 What It Means

Your loop condition never becomes false — your program runs forever 😅.

💥 Example

count = 0
while count < 5:
    print(count)
    # forgot to increment count
Enter fullscreen mode Exit fullscreen mode

💀 Result

This loop never ends, flooding your console.

✅ Fix

count = 0
while count < 5:
    print(count)
    count += 1
Enter fullscreen mode Exit fullscreen mode

💡 Final Thoughts

Error messages can look scary at first, but once you learn to read them like clues, debugging becomes an adventure, not frustration.

The next time your code throws an error — don’t panic.
Take a breath, read carefully, and debug with intention.


🧑‍💻 If this article helped you, drop a ❤️ and follow me for more bite-sized debugging lessons every week!

python #debugging #webdev #beginners

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
crin_zy_f8607dc9e06120b16 profile image
crin zy

Wow ,it's really so good and it's easily understandable .. thank you so much for your guidance.