Python Forum

Full Version: Python Logic Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all. I came across a really weird error in python. I have tried updating to the latest version of python. When the following code is run, the result is python printing 'not equal'. The same happens when using integers. If the logic is changed to equal to (==) in both statements, it works correctly. I have also tried (is not).
status = 'inactive'
if (status != 'inactive' or status != 'falsepos'): print('not equal')
else: print('equal')
status = 'inactive'
status != 'insactive' is False
status != 'falsepos' is True
therefor False or True is True
thus print('not equal')
change if (status != 'inactive' or status != 'falsepos'):
to status != 'inactive' and status != 'falsepos' and you will get False which is what you want
python did exactly what you asked it to do.
That is the correct result. status != 'falsepos' is True. False or True == True. In fact the statement will always be True and will always print 'not equal' no matter what value is assigned to status because at least one of the != has to be True

What do you want the result to be?
Thank you both. I see my error now :)