Why comparing two values with one variable with NOT Equal to is not working in Python

Question:

I am trying to compare a variable with two values not equal to but is not working

if 'Home' or 'Away' not in sku:
    print(sku)
    data = [sku, price]
else:
    print("Error")

2nd way

if sku!="Home" or sku!="Away":
    print(sku)
    data = [sku, price]
else:
    print("Error")

What I am missing here? Why it is passing nevertheless to data=[sku,price] in both cases?

Asked By: user10358702

||

Answers:

In the first way if 'Home' ... will always evaluate to True so you need some rearranging of your logic:

if not ('Home' in sku or 'Away' in sku):
    print(sku)
    data = [sku, price]
else:
    print("Error")

In the second way sku will never be both ‘Home’ and ‘Away’ which is how your logic currently reads, so this will always be True as well.

if sku!="Home" and sku!="Away":
    print(sku)
    data = [sku, price]
else:
    print("Error")
Answered By: azundo
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.