Skip to content Skip to sidebar Skip to footer

Is It Possible To Find Which Condition Has Failed In A Single If/else Statement With Multiple Conditions?

Very shortly I need to verify if 3 conditions are verified and if not execute something regarding the failed conditions. I know I can iterate through the 3 conditions with multiple

Solution 1:

I know I can iterate through the 3 files with multiple if/else statements

Every time you notice a repetition like this in a programming problem, it's a pretty good sign you can use a cycle:

for filename in("1.txt", "2.txt", "3.txt"):
    if not file_exist(filename):
        create_file(...)

You could also use a list comprehension:

[create_file( filename ) for filename in("1.txt", "2.txt", "3.txt")if not file_exist(filename)]

This is closer to the way you read it in english, but some people will frown upon it, because you're using a list comprehension to cause side-effects, instead of actually creating a list.

Solution 2:

No, it's not possible. The if statement has only one condition, in this case that condition is condition1 and condition2 and condition3. It doesn't "remember" the results of sub-expressions in that expression, so unless they have side-effects you're out of luck.

Also be aware that if condition1 is false then it doesn't evaluate condition2 at all. So if you wanted to know which conditions (plural) failed, then and would be entirely the wrong tool for the job. You could instead do something like:

results = (condition1, condition2, condition3)
ifall(results):
    passelse:
    # look at the individual values

In practice, though, if you're going to "do something" for each false value when you look at the individual values, then you don't need to special-case them all being true. Just execute the same code doing nothing at each step.

I suppose that just to prove a point, you could do something peculiar to record the first failure:

def countem(result):
    if result:
        countem.count += 1return result
countem.count = 0ifcountem(condition1) and countem(condition2) and countem(condition3):
    pass
else:
    print countem.count

Or get rid of the if to be a tiny bit more concise:

conditions = (lambda: condition1, lambda: condition2, lambda: condition3)
first_failed = sum(1for _ in itertools.takewhile(lambda f: f(), conditions))

Of course this is not sensible code for your example, but as far as it goes, it handles the general case.

Solution 3:

Instead of just using an if/else solution:

for file_name in('1.txt', '2.txt', '3.txt'):
    try:
       with open(file_name): # default mode 'r' to read file
           #do something... ornot
    except IOError:
       with open(file_name, 'w') as f:
           #something to do...

#modes can be 'w', 'a', 'w+', 'a+'for writing, appending,write/read, append/read respectively. There are others...

There is also:

import os.path

file_path = '/this path if all files/ have the same path.../'for file_name in('1.txt', '2.txt', '3.txt'):
    ifos.path.exists(file_path/file_name):
        continue
    else:
        #create the file


# though os.path.exists this will return True for directories also

Solution 4:

I was just going to say it looks like a for loop to meet certain conditions.

try putting in print statements to see if each condition is being met...simple but effective.

Post a Comment for "Is It Possible To Find Which Condition Has Failed In A Single If/else Statement With Multiple Conditions?"