Introduction to Python ValueError
Python ValueError occurs when there is an issue with the content of the object to which you wanted to assign a value to. These Python ValueErrors are built-in exceptions in the Python programming language. The syntax for Python ValueError is:
List = [1,2,….n]
[1…n] = list
Print(n)
Where,
- n is the number of values we add inside the brackets.
- When we do not assign these values to the proper variables, we end up getting a ValueError in Python.
- Python ValueError is raised when capacity gets a contention of the right kind yet ill-advised worth.
- We can commit certain errors while composing a program that leads to blunders when we attempt to run it. A python program ends when it experiences an unhandled blunder.
How ValueError works in Python?
Now we see how these ValueErrors work in Python. At times, you may need to unload the components or qualities from a rundown. You can dole out the removed qualities into factors or another new rundown. In any case, if the quantity of rundown components is more than the number of factors, a mistake will be raised during the task. The mistake is a ValueError with numerous qualities to unload in Python will be tossed by Python. For example, envision you have a dog, and you attempt to place it in a fish tank. This would be a case of a kind mistake since creatures of type ‘hound’ surely are not equivalent to creatures of type ‘fish’. Then again, envision we attempted to place a Great Dane into a Labrador’s pet hotel. This would be an issue with the pooch’s estimation, on the grounds that despite the fact that they are both of type ‘hound’, a Labrador’s pet hotel would not have the option to acknowledge a canine the size of a Great Dane.
Example #1
Code:
List = [1,2,3,4,5]
a,b,c = List
print(a)
print(b)
print(c)
Output:
Explanation: In the above program, we assign values to a variable named list. The numbers assigned have to be printed. There are totally of 5 numbers in the array, and here we assign only 3 variables to these numbers. Hence the error occurs and asks us to assign more variables to the given values because there are no sufficient values that are assigned in the code.
Example #2 – Defining an array inside an array
Code:
list = [[1,2],[3,4],[5,6],[7,8],9]
a,b,c = list
print(a)
print(b)
print(c)
Output:
Explanation: In the above program, we try to assign an array inside an array. Here, we create an array of numbers and initialize the values of each array. Yet again, we try to assign variables to these array values, and the number of variables assigned here is less compared to the rest of the array values. Hence, the error occurs in the output.
How to avoid ValueError in Python?
Now we see the solutions to the errors that occurred above and how to avoid these exceptions in Python. The best way to dodge the ValueError is by coordinating the number of factors and the number of rundown components. You may likewise utilize a circle to emphasize the components and print them individually.
Example #1
Code:
List = [1,2,3,4,5]
a,b,c,d,e = List
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
In the first solution example, we first decide how to prevent a simple array value exception by just assigning all the variables to the respective array numbers and printing all the variables to which these array values have been assigned to. Hence, the problem is fixed, and we get the above output.
Example #2
Code:
list = [[1,2],[3,4],[5,6],[7,8],9]
a,b,c,d,e = list
print(a)
print(b)
print(c)
print(d)
print(e)
Output:
In this above solution, we see that we assign multiple values when we declare an array inside an array. It can be integers, strings and many more. Hence, similar to the previous solution, we need to assign and print all the 5 values of the given list of arrays to avoid the ValueError exception. Though here there is an array inside an array, the code considers the sub-array as a single entity and prints the output.
The translator or inherent capacities can produce implicit exemptions. But where referenced, they have a “related worth” showing the definite reason for the mistake. This might be a string or a tuple of a few things of data (e.g., a mistake code and a string clarifying the code). The related worth is normally passed as contentions to the special case class’ constructor.
Conclusion
In Python, the worth is data that is put away inside a specific item. To experience a ValueError in Python implies that is an issue with the substance of the article you attempted to allocate the incentive to. This is not to be mistaken for types in Python. Hence, Python ValueError is raised when capacity gets a contention of the right kind; however, it an unseemly worth it. Additionally, the circumstance should not be portrayed by a progressively exact exemption, such as IndexError.
Recommended Articles
We hope that this EDUCBA information on “Python ValueError” was beneficial to you. You can view EDUCBA’s recommended articles for more information.