×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

python3 if not equal condition in OR statement

python3 if not equal condition in OR statement

(OP)
Hey guys,

Trying to figure out why my "not equals" operator doesn't work in this OR statement:

CODE

#!/usr/bin/env python3

import sys

if ans != "y" or ans != "yes":
else:
print("Executing scripts!") 

Whatever I input, it always returns True. If I type in 'y' or 'yes', it should execute my else statement *scratches head*. I've tried putting my conditions into ()'s and such and no dice. Someone please show me the light :). Thanks!

RE: python3 if not equal condition in OR statement

Hi

Quote (twantrd)

ans != "y" or ans != "yes"
So either not "y" or not "yes" ? Let us reverse this : could you tell me a word that is both equal with "y" and with "yes" ?

I think you get it already - you need and there : ans != "y" and ans != "yes".

In case you are ( excuse my assumption ) beginner, you may find easier to use equality checks instead then negate the entire expression : not (ans == "y" or ans == "yes"). ( I used them this way in my early years. YMMV. )

Feherke.
feherke.ga

RE: python3 if not equal condition in OR statement

(OP)
Ah, I completely missed that. Thank you for clearing that up!

RE: python3 if not equal condition in OR statement

personally I would not check for a "Not" condition unless absolutely necessary as it makes code harder to read & maintain

[code]
if a=='y' of a=='yes':
do stuff
else:
exit

A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear

RE: python3 if not equal condition in OR statement

Hi

Some coding standards says that if any of the branches does effectively nothing just exits, then that should be in the "then" branch and there should be no "else" branch. Personally I tend to agree with this rule as I find it makes my code easier to follow.

CODE --> Python

if ans != "y" or ans != "yes":

print("Executing scripts!") 

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!