Python Forum
a stack of elifs and a try/except - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: a stack of elifs and a try/except (/thread-21057.html)



a stack of elifs and a try/except - Skaperen - Sep-12-2019

while testing what a given string means by applying a few tests in a stack of elifs the next test is to see if giving it to a function will or will not throw an exception. is there a good way to include such a test in the stack of elifs? a couple of examples for the function include int() and float() but it could be something else.


RE: a stack of elifs and a try/except - ichabod801 - Sep-12-2019

I usually do any conversion with try/except first, then run through any if/elif chain for testing if it is valid after being converted.


RE: a stack of elifs and a try/except - Skaperen - Sep-12-2019

the conversion needs to be done only if certain tests fail. those tests are the first ones in the elif chain. it looks like the way to do this is to isolate that test in a function so it fits in the elif chain as a simple result.