1

I know there's lots of documentation about the OR operator, but I cant get it to work in my Python Code. I am a beginner so lots of python syntax/methods are new to me.

def getDecision():
x = input()
while x != ("a" or "b"):
    print("Please input \"A\" or \"B\"")
    x = input()
return x

Currently it only checks if x is not equal to A, it seems to ignore B. This is different to other questions as if I write

while (x != "a") or (x != "b")

That will it will go with the loop if A or B is typed in

Many Thanks

Tom
  • 33
  • 6
  • 1
    You may also find http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response useful. – Ffisegydd Oct 28 '14 at 13:52
  • 1
    Try `while x not in ('A', 'B'):` – Matt Oct 28 '14 at 13:54
  • I disagree with marking this as a duplicate. Because this is `!=` rather than `==`, the logic of the question is more difficult than the duplicate. Here, Matt's answer is needed, or a solution that turns the `or` to an `and`: `x!="a" and x!="b"`. – tom10 Oct 28 '14 at 13:59
  • `or` is a logical operator. – Karoly Horvath Oct 28 '14 at 14:00
  • 1
    "I know there's lots of documentation about the OR operator" - perhaps you should read them?! – Karoly Horvath Oct 28 '14 at 14:02
  • Thanks Matt, that worked! Sorry if it seemed like a duplicate to some. – Tom Oct 28 '14 at 14:04
  • It did seem so but I found your specific situation was not addressed in the 'duplicate' link, so I flagged this question to reopen – Tim Oct 28 '14 at 14:08

3 Answers3

2

This should probably be:

while x!="a" and x!="b":

or

while x not in ("a", "b"):

The first is the logical equivalence of your statement, since ~(a or b) == ~a and ~b. The second is the more Pythonic way to write this, and is also easier to understand.

tom10
  • 67,082
  • 10
  • 127
  • 137
  • 2
    I think, for new users, it's more important to explain why something happens, than just how to fix it – Tim Oct 28 '14 at 14:09
  • @TimCastelijns: Then please feel free to do that. – tom10 Oct 28 '14 at 14:10
  • I'll admit I don't exactly know why it happens, but I figured you would, being a gold python badge owner :-) – Tim Oct 28 '14 at 14:15
  • @TimCastelijns: Personally, I think the problem displayed in the OP's question is not in failing to understand exactly what Python does with the incorrect expression, but more in understanding that `==` and `!=` does not distribute over a logical expression. It seems to me that this is much better illustrated by the simple example of this question and answer than by more words attached to a mathematical description vs how this relates to a similar Python expression. This is why I gave both the logical equivalent (for understanding) as well as the more Pythonic way to write this. – tom10 Oct 28 '14 at 14:22
  • Fair enough :-) I trust you to be the better judge, because you have a lot more experience – Tim Oct 28 '14 at 14:27
2

The problem comes from this part:

x != ("a" or "b")

The expression ("a" or "b") will always evaluate as "a" because what it does is to check first "a" (first variable) is True/False (In python empty string is evaluated as False, and the rest as True). Because "a" will be evaluated as True, it won't even check the second variable and will just return "a". Moreover, this expression in particular checks if any of the given variables evaluates as True and returns the first one which is (in your case "a").

A solution will be to change to

while x not in ('a', 'b')

as mentioned by other users.

go2
  • 378
  • 4
  • 14
0

If you want the while loop to be in the function then you need to indent it. also you should use either:

while x not in ('a', 'b') or while x!="a" and x!="b":

TheUnknown
  • 33
  • 3