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?
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")
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?
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")