Home > OS >  How to handle multiple value error in python?
How to handle multiple value error in python?

Time:05-21

ValueError: not enough values to unpack (expected 3, got 2)
    
ValueError: too many values to unpack (expected 3)

these are the two differant error so how we can throw error like

for first ValueError I want to print ==> not enough value and for second ValueError print ==> extra values

how I can handle this error using exception method or anything other in python

CodePudding user response:

Try this,

a = (1,2,3)  # a = (1,)
try:
    b,c = a   #Try to unpack
except ValueError as e:
    print(e)
    if('not enough values to unpack' in str(e)):
        print("not enough value")
    if('too many values' in str(e)):
        print("extra values")
  • Related