I am trying to create an if else code where there is about 20conditions for the elif, how do I create a list of conditions where i can just type something such as:
uno= <9
lol= >20
crad= =<3
list={uno,lol,crad}
if 13=list:
print("yay")
elif 13!=list:
print("nay")
That's my current code It should print "yay", instead there is syntax error
3 Answers 3
It's not actually simpler than writing a chain of if/elif/elif etc, but something like this seems to do what you are asking:
predicates = [lambda x: x<9, lambda x: x>20, lambda x: x<=3]
if all(y(13) for y in predicates):
print("yay")
else:
print("nay")
Each predicate is a small anonymous function (a lambda) which receives a single argument and evaluates to either True or False. If you have a large number of arguments you want to check against a large set of predicates, it's nice to be able to encapsulate the predicates like this. The ability to programmatically add or remove predicates from the list truly extends the versatility of this construct beyond what you can (easily, naturally) do with if/elif/elif.
This particular set of predicates can't all be true for a single number. Maybe you want any() instead of all()...?
Comments
Your "conditions" are functions mapping the input to booleans. Therefore, you can write them as functions:
def is_small(number):
return number < 9
def is_large(number):
return number > 20
conditions = (is_small, is_large)
Then you can evaluate all of these functions on some input:
def check_all(input, conditions):
return [condition(input) for condition in conditions]
check_all(10, conditions)
>>> [False, False]
And if you want to know if all of these or any one of these are true, you can use the functions all and any:
any(check_all(10, conditions))
>>> False
any(check_all(21, conditions))
>>> True
And not all(...) is True if one of the conditions is not fulfilled, not any is True if none is.
Edit: One thing to notice is that the list comprehension [... for ... in ...] in check_all always evaluates all functions. This is not necessary if you use any or all, which can use an iterator and stop evaluating it onces the result it fixed (at the first True for any and the first False for all). If you use them, you can just replace the list comprehension [... for ... in ...] by a generator expression (... for ... in ...).
2 Comments
any is that it won't evaluate the rest of the list as soon as it finds an item which returns True. It would be nice if you could use this shortcut logic, so I would remove the check_all() wrapper, or refactor it to use any() or all() depending on how you use it.You could create a tuple of booleans and check it via all.
conditions = []
conditions.append(yourVar < 9)
conditions.append(yourVar > 20)
conditions.append(yourVar <= 3)
if all(conditions):
print('All conditions are met')
else:
print('At least one condition results in false)
anyorallfunction to check them all.if..elif...elsecode, and then we can show you a more compact way of doing it (if there is one).