DEV Community

Cover image for A Guide to Solving Common Python Errors for Beginners
Artur Meyster for Career Karma

Posted on

A Guide to Solving Common Python Errors for Beginners

A Guide to Solving Common Python Errors for Beginners

Errors are a natural part of programming. Unlike humans, programming languages cannot gloss over tiny mistakes like typos. Languages need us to fix any problems in our code before they can continue to run our code.

As a beginner, errors can seem intimidating. To make matters worse, Python displays their errors in red. What’s scarier than a complicated red message that includes code and sometimes technical words that you may not have heard of?

We’ve decided to compile a list of top Python errors that beginners encounter. This list spans from name errors to type errors so that you’ll be prepared to handle a vast array of different problems in your code. Let’s dive into the top ten errors that Python beginners encounter!

nameerror: name is not defined

Name errors occur when you try to use a variable or a function name that has not been assigned. This problem can be caused by, among other reasons, a misspelled variable or function name, calling a function before declaration, or forgetting to define a variable or function.

typeerror: string indices must be integers

In Python, strings can be accessed using indexing. This is where you specify the index number of the item in a string that you want to access. Index numbers must be stated as numbers. You can solve this error by accessing values in a string using integer index numbers.

typeerror: can’t multiply sequence by non-int of type ‘float’

Integers can be multiplied by strings to create repeating strings. Floating-point numbers cannot be multiplied by a string. To solve this error, you should make sure any numbers are converted to floating points before attempting to multiply them together.

typeerror: ‘int’ object is not iterable

Objects like lists, dictionaries, and tuples are iterable. This means that you can loop through them using a “for” loop. You cannot iterate over a number. This error is commonly raised when you use len() to iterate through a list of numbers but forget to use a range() statement.

typeerror: list indices must be integers or slices, not str

Like strings, lists can be accessed using indexing and slicing. The index number you specify to access an item from a list must be an integer. If it is a string, this error will be raised. To solve this error, convert any index numbers to integers before using the indexing or slicing syntax.

typeerror: can only concatenate str (not “int”) to str

Strings can only be merged, or “concatenated,” with other strings. This is because the concatenation operator (+) is treated as an addition sign for numerical operations.

To solve this error, convert all values that you want to concatenate to the string data type using the str() method.

‘str’ object does not support item assignment

Strings are immutable objects. This means that they cannot be changed after they are declared. You cannot use indexing to change characters in a string like you can with a list. This error can be solved by assigning a modified string to a new variable.

indexerror: list assignment index out of range

You can only assign an item to a list if the position to which you want to assign that item exists. You can solve this error by making sure that you only assign items in a list if there is already a value in the specified position. You can also initialize a list with the right number of values before using it to resolve this issue.

syntaxerror: EOL while scanning string literal

This error is raised when you forget to close a string by the end of a line.

This error is commonly caused by mismatching quotation marks (i.e. a single opening quotation mark and a double ending quotation mark), missing quotation marks, or multi-line strings that do not use three quotation marks (“““ or ‘‘‘).

typeerror: ‘int’ object is not subscriptable

Objects that contain other objects, like lists, are subscriptable. Integers are not subscriptable because they do not contain other objects.

This means that you cannot access values in an integer using slicing or indexing. To solve this error, make sure an integer is converted to a subscriptable object like a list or a string before trying to access values using slicing or indexing.

Conclusion

There are plenty more Python errors that we have not covered. There’s a lot of ways that developers make mistakes in their code and Python has to account for all of them that affect the way the Python interpreter reads code.

Do not let error messages hold you back from coding. They’re a natural part of programming. Once you get some practice solving an error, you’ll find it gets easier to identify the cause of and solve similar errors in the future.

Top comments (0)