Search code examples
pythonstringequalityinequality

Checking the inequality of several variables in python without knowing their value


I have four string variables: a,b,c,d. I can't know their values (they are assigned randomly), and I don't care. I only need to make sure that every single on of them is different from others and there are not two variables with the same value. I tried

if a != b != c != d:
    return true

but it doesn't work. What should I do?


Solution

  • Do this?

    assert len({a, b, c, d}) == 4 # set of 4 distinct elements should be 4
    
    if len({a, b, c, d}) == 4:
        return True
    

    The reason your conditions don’t work

    >>> 1 != 2 != 1
    True
    

    From the above example, 1 != 2 and 2 != 1, hence passing the condition. It doesn’t check your first and third variable's equality.