1

I was a bit curious if there's something that I was missing in the code below,

The logic I want the user to enter either "Y" or "H" or "E", which I would like to transfer to another class using a return statement. I use the else part to return boolean False if otherwise.

There is a while loop that would send the control back to this same function until the user enters the required value.

On running the program it never enters the else part no matter what input is received.

def getUserIngameOption(self):

    userIngameOption=raw_input("Please Enter Y -> for Turn | H -> Hint | E -> to End")        
    userDesc=userIngameOption.upper()


     if not ( userDesc==("Y") or ("H") or ("E")):

         print "Mate PLEASE JUST TYPE WHATTTT is asked for !! not this",userDesc
         return False
     else:
         self.userOpt=userDesc
         print "This is detail that you got>>>",self.userOpt
         return self.userOpt
RoadieRich
  • 6,330
  • 3
  • 35
  • 52
Smple_V
  • 838
  • 3
  • 13
  • 32
  • 2
    The original title for this question is "Using multiple bitwise operator in python." Just as a comment, this is a **logical** `or`, not a **bitwise** `or`. – Deacon Apr 01 '16 at 18:23

4 Answers4

7

You arent making the comparison correctly. You are ORing the result of the first comparison with the boolean value of 'H' and 'E'. Since 'H' and 'E' have a boolean value of True, the IF statement will always be False (since you are inverting the condition's result with not).

Change this:

if not (userDesc==("Y") or ("H") or ("E"))

to:

if userDesc not in ("Y","H","E"):
SoreDakeNoKoto
  • 1,175
  • 1
  • 9
  • 16
2

What is happening is that each one is being evaluated separately, you will need to explicitly compare each value.

if not ( userDesc=="Y" or userDesc=="H" or userDesc=="E")

"E" and "H" will evaluate to True in your current code.

Tom Myddeltyn
  • 1,307
  • 1
  • 13
  • 27
1

This the the correct method :

if not ( userDesc=="Y" or userDesc=="H" or userDesc=="E")

or this :

if  userDesc  not in ("H","Y","E").
formatkaka
  • 1,278
  • 3
  • 13
  • 27
0

if not ( userDesc==("Y") or ("H") or ("E")) means (userDesc==("Y")) or (("H")) or (("E")) and the second and third terms are always true.

Haochen Wu
  • 1,753
  • 1
  • 17
  • 24