Back to Blog
Developer guide
PYTHONJune 14, 2026

Python ValueError Examples and Fixes for Developers

Python developers often encounter ValueError exceptions when working with data, input validation, or string operations. ValueError errors occur when a function or method receives an argument with an incorrect data type or value. This article covers various Python ValueError examples and provides step-by-step fixes to help you resolve these issues efficiently. By understanding the root causes of ValueError errors, you'll be better equipped to write robust and error-free code.

1. ValueError: invalid literal for int() with base 10

This error occurs when attempting to convert a non-numeric string to an integer using the int() function.

Why It Happens

The error occurs when the input string contains non-numeric characters or when the string is empty.

How to Fix It

To fix this error, ensure that the input string is a valid integer representation. You can use a try-except block to catch and handle ValueError exceptions. Alternatively, use the str.isdigit() method to verify if the string is a digit before attempting to convert it to an integer.


2. ValueError: not enough values to unpack

This error occurs when attempting to unpack a tuple or list with fewer elements than expected into multiple variables.

Why It Happens

The error occurs when the number of elements in the tuple or list does not match the number of variables in the assignment.

How to Fix It

To fix this error, ensure that the number of elements in the tuple or list matches the number of variables. You can use the len() function to get the length of the tuple or list and compare it with the number of variables. Alternatively, use the * operator to unpack the remaining elements into a list.


3. ValueError: malformed node or edge

This error occurs when attempting to create a graph using the NetworkX library with malformed edge or node data.

Why It Happens

The error occurs when the edge or node data contains invalid or incomplete information.

How to Fix It

To fix this error, ensure that the edge or node data is in the correct format. Check the NetworkX library documentation for the expected input format. Additionally, use the try-except block to catch and handle ValueError exceptions.


4. ValueError: time data did not match format

This error occurs when attempting to parse a date or time string using the datetime.strptime() function with an incorrect format.

Why It Happens

The error occurs when the date or time string does not match the specified format.

How to Fix It

To fix this error, ensure that the date or time string matches the specified format. You can use the datetime.strptime() function with different formats to find the correct one. Alternatively, use the strptime() function with the format code '%Y-%m-%d' or '%Y-%m-%d %H:%M:%S' to parse date and time strings in a flexible manner.


5. ValueError: array must not be empty

This error occurs when attempting to perform an operation on an empty array using the numpy library.

Why It Happens

The error occurs when the array is empty or has no elements.

How to Fix It

To fix this error, ensure that the array is not empty before performing the operation. You can use the len() function to get the length of the array and check if it is greater than 0. Alternatively, use the try-except block to catch and handle ValueError exceptions.


6. ValueError: invalid truth value

This error occurs when attempting to use a non-boolean value in a boolean context, such as in an if statement or a conditional expression.

Why It Happens

The error occurs when the value is not a boolean (True or False) but is being used as one.

How to Fix It

To fix this error, ensure that the value is a boolean (True or False) before using it in a boolean context. You can use the bool() function to convert the value to a boolean. Alternatively, use the if-else statement to handle the value explicitly.


7. ValueError: maximum recursion depth exceeded

This error occurs when a function calls itself recursively too many times, exceeding the maximum recursion depth.

Why It Happens

The error occurs when the recursion depth is too high or when the function has no base case to stop the recursion.

How to Fix It

To fix this error, ensure that the function has a base case to stop the recursion. You can use the sys.setrecursionlimit() function to increase the maximum recursion depth, but be cautious not to increase it too much. Alternatively, use an iterative solution instead of recursion.

Conclusion

ValueError errors in Python can occur due to various reasons, including incorrect data types, malformed input, or excessive recursion. By understanding the root causes of these errors and applying the fixes outlined in this article, you'll be better equipped to write robust and error-free code. Remember to use try-except blocks to catch and handle ValueError exceptions, and to validate input data to prevent errors from occurring in the first place.

Explore More Debugging Resources

- [Browse all PYTHON errors](/languages/python)

- [Browse errors by type](/error-types)

- [Search all documented errors](/search)

- [Use the Error Explainer](/error-explainer-tool)

Browse allPython errors

Related PYTHON Articles

Have a specific error? Get an instant explanation.

Explain an Error