Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Logic Error
#1
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')
Yoriz write Dec-05-2022, 12:06 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
rmfooty likes this post
Reply
#3
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?
rmfooty likes this post
Reply
#4
Smile 
Thank you both. I see my error now :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  logic python JunkBoy 5 1,557 Aug-01-2022, 05:13 AM
Last Post: JunkBoy
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,898 Sep-18-2019, 09:43 AM
Last Post: Larz60+
  Why is my crawler not recording 404s I think it may be an error in the logic RickyWilson 3 3,394 Dec-18-2017, 12:57 PM
Last Post: Larz60+
  Logic error when comparing randomly generated integers SadoDeomeoon 5 4,680 Jun-05-2017, 02:38 PM
Last Post: SadoDeomeoon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020