-1

I have this:

salir = ""
while  salir != "s" or salir != "S":
       print("no has salido")
       salir = input("Digit s to exit or enter to continue)
print("saliste")

but the operator != doesnt work, but if I put this:

salir = ""
while  not (salir == "s" or salir == "S"):
       print("no has salido")
       salir = input("Digit s to exit or enter to continue")
print("saliste")

the code works normal, the problem is the comparison operator != because if I change that by " not == " this works. Can anybody explain the problem?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
HAM
  • 11
  • 3

4 Answers4

2
while  salir != "s" or salir != "S":

If salir is "s" it's different than "S", if it's "S", it's different than "s". So, your condition is always true.

If we change the letters make it easier to understand.

while salir != "a" or salir != "b":

While your variable is different than a or is different than b, it always will be different than one of the two options, so the condition is always true.

You can do a single comparison just changing the case of the variable, like:

while salir.lower() != "s":
dfranca
  • 5,156
  • 2
  • 32
  • 60
0

The expression you are evaluating always returns True

salir != "s" or salir != "S" == True  # always

If the user inputs 's', then salir != "S"

If the user inputs 'S', then salir != "s"

If you want to split the two cases (instead of calling the lower() method) you can use the loop

while  salir != "s" and salir != "S":
    # Do stuff

By DeMorgan's law this is equivalent to

while  not (salir == "s" or salir == "S"):
    # Do stuff
Alex
  • 18,484
  • 8
  • 60
  • 80
  • 1
    Oh Thanks, I dont know how I forget evaluate the complete expressions as it should be, I guide myself just to evaluate each part of them but with fogetting sense :) , Thanks Alex. – HAM Mar 14 '15 at 15:13
0

You could also do the following:

salir = ""
while True:
  print("no has salido")
  salir = input("Digit s to exit or enter to continue")
  if salir.lower() == "s": break
print("saliste")
Alexandru Godri
  • 528
  • 3
  • 7
0

If you fancy regular expressions, I absolutely love the "re" module.

import re

def main():
    counter = 0
    taco = "q"

    while re.search(re.compile(r"s",re.IGNORECASE),taco) == None:
        print("no")
        if counter == 5:
            print("taco = s")
            taco = "S"
        else:
            pass
        counter += 1

if __name__ == '__main__':
    main()

Which gives the output:

no
no
no
no
no
no
taco = s

While this will use more CPU cycles, it does allow for some greater flexibility with having multiple cases, or options, in a custom designed regular expression. I usually use expressions in most of my code so I don't feel as bad for wasting a few extra CPU cycles.

re.search will search the string looking for the argument where-as re.match would require a string to match your expression completely; these would return a re object. re.findall would return a list of results so one could do "while re.findall(...) == 0:" as well.

Please refer to the "re" documentation for more information: https://docs.python.org/2/library/re.html

MuffintopBikini
  • 1,042
  • 7
  • 11