Tuesday, November 19, 2019

try and except method in python

l=[10,20,30,40,10,20,10,10]
target=int(input("enter the value to search"))
try:
    print(target,"available and its first occurence is at:",l.index(target))
except ValueError:
    print(target,"not available")

output
enter the value to search20
20 available and its first occurence is at: 1


enter the value to search30
30 available and its first occurence is at: 2


enter the value to search50
50 not available

No comments:

Post a Comment

python class topic video