Lesson 18 of 18 · 2 min
Common Errors & Fixes
| Error Message | Why it happens | How to fix |
|---|---|---|
NameError: name 'x' is not defined | You used a variable before ever assigning a value to it | Create the variable first: x = 5, then use it |
TypeError: can only concatenate str (not "int") to str | You tried to join a string and a number using + — Python refuses to mix types | Convert the number to a string first: str(42), or use an f-string instead |
SyntaxError: invalid syntax | Forgot quotes around text, used = instead of == in a comparison, or missing parenthesis | Add quotes around strings; use == to compare values, = only to assign |
ValueError: invalid literal for int() with base 10: 'abc' | Called int() on text that is not a valid number, e.g. int("twenty") | Check the value is numeric before converting, or wrap in try/except ValueError |
IndentationError: unexpected indent | Added spaces or a tab somewhere Python did not expect them | Remove the accidental indentation, or be consistent (always use 4 spaces) |
ZeroDivisionError: division by zero | Divided a number by 0, which is mathematically impossible | Check if denominator != 0: before the division operation |
