Home > Enterprise >  If just one conditional is false then python
If just one conditional is false then python

Time:09-15

I have a question related to if statement. I have multiple conditions as below example:

if a==a or b==b or c==c or d==d or e==e or f==f or g==g:
    print("the same")

but i would also like to see if one of the conditions may be false but no matter which. Example:

cond = ['True', 'True', 'True', 'True', 'True', 'True', 'True']
# print("the same")

cond = ['False', 'True', 'True', 'True', 'True', 'True', 'True']
# print("Not the same, but one is ok")

cond = ['True', 'True', 'True', 'True', 'False', 'True', 'True']
# print("Not the same, but one is ok")

cond = ['True', 'True', 'True', 'False', 'False', 'True', 'True']
# print("Not the same")
 

CodePudding user response:

Not sure if using strings to represent True/False is a great idea but in the context of the question then this will work:

def check_cond(cond):
    assert cond.count('True') cond.count('False') == len(cond)
    assert 'True' in cond
    if cond.count('False') > 1:
        return 'Not the same'
    return 'the same' if len(set(cond)) == 1 else 'Not the same, but one is ok'

conditions = [['True', 'True', 'True', 'True', 'True', 'True', 'True'],
              ['False', 'True', 'True', 'True', 'True', 'True', 'True'],
              ['True', 'True', 'True', 'True', 'False', 'True', 'True'],
              ['True', 'True', 'True', 'False', 'False', 'True', 'True']]

for cond in conditions:
    print(cond, check_cond(cond))

Output:

['True', 'True', 'True', 'True', 'True', 'True', 'True'] the same
['False', 'True', 'True', 'True', 'True', 'True', 'True'] Not the same, but one is ok
['True', 'True', 'True', 'True', 'False', 'True', 'True'] Not the same, but one is ok
['True', 'True', 'True', 'False', 'False', 'True', 'True'] Not the same

CodePudding user response:

Variant to be able to display a custom message on any specific count, else a default message:

def check(cond):
    return {
        0: 'the same',
        1: 'Not the same, but one is ok'
    }.get(sum(x=='False' for x in cond),
          'Not the same'  # default message
         )
          
check(['True', 'True', 'True', 'True', 'True', 'True', 'True'])
# 'the same'

check(['False', 'True', 'True', 'True', 'True', 'True', 'True'])
# 'Not the same, but one is ok'

check(['True', 'True', 'True', 'True', 'False', 'True', 'True'])
# 'Not the same, but one is ok'

check(['True', 'True', 'True', 'False', 'False', 'True', 'True'])
# 'Not the same'

with booleans

NB. it would be much better to use booleans instead of strings!.

def check(cond):
    return {
        0: 'the same',
        1: 'Not the same, but one is ok'
    }.get(len(cond)-sum(cond),
          'Not the same'
         )
          
check([True, True, True, True, True, True, True])
# 'the same'

check([False, True, True, True, True, True, True])
# 'Not the same, but one is ok'

check([True, True, True, True, False, True, True])
# 'Not the same, but one is ok'

check([True, True, True, False, False, True, True])
# 'Not the same'
  • Related