Python Forum
a stack of elifs and a try/except
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a stack of elifs and a try/except
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simplifying a stack of elifs Skaperen 8 3,996 Aug-17-2019, 04:13 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020