Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raise ValueError for multiple if-else

if city == 'Chennai':
   print "this is Chennai city"
elif city == 'Delhi':
   print "this is Delhi city"
else:
   print "invalid city"

I want to raise ValueError whenever city is invalid. How to do that ?

like image 793
Patrick Avatar asked Dec 17 '25 13:12

Patrick


1 Answers

city = 'some city'

if city == 'Pune':
    print "this is pune city"
elif city == 'Delhi':
    print "this is Delhi city"
else:
    raise ValueError("Invalid City")

Traceback (most recent call last):
  File "<pyshell#2>", line 8, in <module>
    raise ValueError('Invalid City')
ValueError: Invalid City
like image 199
Jayanth Koushik Avatar answered Dec 20 '25 10:12

Jayanth Koushik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!