Important Notice:

Value Error

Value Error

26 views 1 min read

ValueError (वैल्यू एरर) :-

ValueError तब आती है जब किसी Function या Operation को सही Data Type दिया जाता है, लेकिन उसकी Value (मान) मान्य (Valid) नहीं होती। अर्थात Data Type सही होता है, पर Value गलत होती है।

English:

A ValueError occurs when a function or operation receives an argument of the correct data type, but the value is not valid for that operation. In other words, the data type is correct, but the value is inappropriate.

 

Wrong Example 1:     Invalid Integer Conversion :-

num = int("Python")

print(num)
 
Output:-
ValueError: invalid literal for int() with base 10: 'Python'
 
Correct Example -
num = int("100")
print(num)
 
Output:-
100
 

Wrong Example 2:  Invalid Float Conversion :-

price = float("ABC")
print(price)
 
Output:-
ValueError: could not convert string to float: 'ABC'
 
Correct Example -
price = float("99.50")
print(price)
 
Output:-
99.5
 

Wrong Example 3:  Value Not Found in List :-

numbers = [10, 20, 30]
print(numbers.index(50))
 
Output:-
ValueError: 50 is not in list
 
Correct Example -
numbers = [10, 20, 30]

print(numbers.index(20))
 
Output:-
1
 
 
 
 

Related Notes