Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • 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.

Students Click Here

python3 if not equal condition in OR statement

python3 if not equal condition in OR statement

python3 if not equal condition in OR statement

Hey guys,

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


#!/usr/bin/env python3

import sys

readyprompt = input("Are you ready? [y/n]\n>")
ans = readyprompt.lower()
if ans != "y" or ans != "yes":
        print("Quitting. Please run " + sys.argv[0] + "when ready")
        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


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

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. )


RE: python3 if not equal condition in OR statement

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

if a=='y' of a=='yes':
do stuff

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


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":
        exit("Quitting. Please run " + sys.argv[0] + "when ready")

print("Executing scripts!") 


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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close