Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

"Return" a value if a specific Error occurs

$
0
0

I am creating n variables, and whenever a Error occur, i want to give a specific value to the variable.Example:

a = "abc"b = 0def lastnext (word, count):    last = word[count - 1]    next = word[count + 1]    return last,nextprint(lastnext (a,b))

This way, whenever b = (len( a ) - 1) (in this case b = 2), the variable next would give me a IndexError, because theres no word[ 3 ]. Then, i want to "return" a specific value to the variable, like "None" .

I thought about using the try and except method to every single variable, but i guess it would be a whole different story with n variables.


Viewing all articles
Browse latest Browse all 23218

Trending Articles