Back to Blog
Developer guide
PYTHONJune 26, 2026

Python ValueError Examples and Fixes for Developers

When working with Python, developers often encounter the ValueError exception, which occurs when a function or operation receives an argument with an incorrect or unexpected value. In this article, we'll delve into Python ValueError examples and provide actionable fixes for common scenarios. By understanding the causes and solutions to these errors, you'll be able to write more robust and error-free code. Whether you're a seasoned developer or just starting out, this guide will help you navigate the world of Python ValueError errors and improve your coding skills.

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

This error occurs when the int() function is used to convert a string to an integer, but the string contains non-numeric characters or is not a valid integer representation.

Why It Happens

This error can occur when trying to parse a string that contains spaces, commas, or other non-numeric characters, or when the string is not a valid integer representation.

How to Fix It

To fix this error, ensure that the string only contains numeric characters and is a valid integer representation. You can use the str.strip() method to remove leading and trailing whitespace, and then use a try-except block to catch and handle any ValueError exceptions.


2. ValueError: not enough values to unpack

This error occurs when trying to unpack a tuple or list into multiple variables, but the number of values in the tuple or list is less than the number of variables.

Why It Happens

This error can occur when the data you're trying to unpack is incomplete or corrupted, or when the number of variables exceeds the number of values in the tuple or list.

How to Fix It

To fix this error, ensure that the number of variables matches the number of values in the tuple or list. You can also use the * operator to unpack any remaining values into a list or tuple.


3. ValueError: max recursion depth exceeded

This error occurs when a function calls itself recursively without terminating, causing the interpreter to exceed the maximum recursion depth.

Why It Happens

This error can occur when a recursive function is not properly designed, or when the base case is not correctly implemented.

How to Fix It

To fix this error, identify the recursive function and ensure that the base case is correctly implemented. You can also use a loop instead of recursion to avoid this error.


4. ValueError: invalid date format

This error occurs when trying to parse a date string using the strptime() function, but the date format is not recognized.

Why It Happens

This error can occur when the date string is not in the expected format, or when the date format is not correctly specified.

How to Fix It

To fix this error, ensure that the date string is in the correct format, and that the date format is correctly specified. You can use the strptime() function with a custom format string to parse the date string.


5. ValueError: invalid literal for float() with base 10

This error occurs when the float() function is used to convert a string to a floating-point number, but the string contains non-numeric characters or is not a valid floating-point representation.

Why It Happens

This error can occur when trying to parse a string that contains spaces, commas, or other non-numeric characters, or when the string is not a valid floating-point representation.

How to Fix It

To fix this error, ensure that the string only contains numeric characters and is a valid floating-point representation. You can use the str.strip() method to remove leading and trailing whitespace, and then use a try-except block to catch and handle any ValueError exceptions.


6. ValueError: too many values to unpack

This error occurs when trying to unpack a tuple or list into multiple variables, but the number of values in the tuple or list exceeds the number of variables.

Why It Happens

This error can occur when the data you're trying to unpack has more values than you're expecting, or when the number of variables exceeds the number of values in the tuple or list.

How to Fix It

To fix this error, ensure that the number of variables matches the number of values in the tuple or list. You can also use the * operator to unpack any remaining values into a list or tuple.


7. ValueError: list index out of range

This error occurs when trying to access an element in a list using an index that is out of range.

Why It Happens

This error can occur when the list is empty, or when the index is greater than or equal to the length of the list.

How to Fix It

To fix this error, ensure that the index is within the valid range for the list. You can use the len() function to get the length of the list, and then use a try-except block to catch and handle any IndexError exceptions.

Conclusion

In this article, we've covered common Python ValueError examples and provided practical fixes for each error. By understanding the causes and solutions to these errors, you'll be able to write more robust and error-free code. Remember to always use try-except blocks to catch and handle any ValueError exceptions, and to ensure that the number of variables matches the number of values in the tuple or list. With practice and experience, you'll become more proficient in debugging and fixing Python ValueError errors.

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