Common Python Errors and How to Fix Them
This article explains the most frequent Python runtime and syntax errors—including IndentationError, TabError, SyntaxError, NameError, IndexError, KeyError, TypeError, and AttributeError—provides clear descriptions of their causes, and offers corrected code examples to help beginners debug their programs effectively.
When writing code, beginners often encounter various bugs that can be confusing and halt program execution.
1. IndentationError occurs when the code is not properly indented; Python requires each indentation level to consist of four spaces. Example of the error and its fix:
<code>a=1<br/>b=2<br/>if a<b:<br/>print a</code> <code>a=1<br/>b=2<br/>if a<b:<br/> print a</code>2. Tab and space mixing (TabError) happens when tabs and spaces are used together for indentation, leading to inconsistent width. The recommendation is to use spaces only.
3. SyntaxError includes invalid syntax, invalid characters, or incomplete strings. Common causes are missing punctuation, mixing Chinese and English symbols, or mismatched quotes. Example:
<code>print( 'hello', 'world')</code>Correct version uses English commas:
<code>print('hello', 'world')</code>Other syntax issues include missing closing parentheses or missing colon after control statements.
<code>if name =="A"<br/>print("hello")</code>Fix by adding a colon:
<code>if name =="A":<br/> print("hello")</code>4. NameError is raised when a variable name is misspelled or not defined. Example:
<code>message = "Hello!"<br/>print(mesage)</code>Correct the variable name:
<code>print(message)</code>5. IndexError occurs when accessing a list element with an out‑of‑range index. Example:
<code>a = [1,2,3]<br/>print(a[3])</code>Valid indices are 0‑2 for this list.
6. KeyError is raised when a dictionary key does not exist. Example:
<code>d = {'a':1,'b':2}<br/>print(d['f'])</code>Ensure the key exists before accessing it.
7. TypeError happens when an operation is applied to an inappropriate type, such as concatenating a string with an integer without conversion:
<code>age=18<br/>print("我的年龄是"+age)</code>Fix by converting the integer to a string:
<code>print("我的年龄是" + str(age))</code>8. AttributeError is raised when trying to access an attribute that does not exist on an object, often due to a typo or missing __init__ definition.
For beginners, encountering these errors is a normal part of learning; each error can be seen as a small obstacle to overcome, similar to defeating monsters in a game.
Keep practicing, and you will become more proficient at debugging your Python code.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.