0

I am making a quick zork game but I ran into this problem using the "or" operator. I thought this would be simple but I can't figure out why this isn't working. Right now if you type in "n" you should get "this works" because it equals the string "n". Instead it prints out "it works" AND "this works" so obviously I used "or" wrong.

   x=0
    while x<20:
        response = input("HI")
        if response!= 'n':
            print("it works")

        if response == 'n':
            print("this works")
        x+=1

Before using or it works

x=0
while x<20:
    response = input("HI")
    if (response!= 'n') or (response != 's'):
        print("it works")


    if (response == 'n') or (response == 's'):
        print("this works")
    x+=1

After using or it prints both out. It probably something obvious -.-

Jotin2
  • 39
  • 3
  • As a side note, for a Zork-style text adventure game, a function for each room (I'm just assuming here, but that's what most novices do…), each of which is a long string of `if`/`elif` statements like this, very quickly gets unwieldy. You might want to consider factoring out the parser from the rest of the code, and using a real parsing library, and turning the rooms into data instead of code, and so on. Building an adventure in something like [Inform](http://inform7.com) first to see how easy it can be, then trying to figure out how to make it that easy in Python, maybe be helpful. – abarnert Dec 18 '13 at 02:05

2 Answers2

4

the expression:

(response != 'n') or (response != 's')

will always be True for any string response. If response is 'n', then it isn't 's'. If it's 's', then it isn't 'n'. If it's anything else, then it's not 's' and it's not 'n'.

Perhaps you meant to use and there?

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 2
    @Jotin2 -- Now now, let's not resort to name calling. I'm pretty sure that most of us have spent hours of our lives baffled by equally simple errors. – mgilson Dec 18 '13 at 01:34
  • @mgilson I totally agree :) – thefourtheye Dec 18 '13 at 01:40
  • @mgilson: Nah, I always make the opposite mistake, and de-morgan it but only half-way and beat my head against the wall trying to debug it. :) – abarnert Dec 18 '13 at 02:01
  • @abarnert -- 9 times out of 10, when I try to de-morgan something I realize that I need to think way harder when going back and reading the code. Ultimately I write it the way that is most like how I would say it unless I have a really good reason to do it differently. – mgilson Dec 18 '13 at 02:03
  • @mgilson: Yeah, but here, the English is "if it's neither n nor s`, which is easier as "if it's not either of n or s" than "if it's not n and also not s", which is why I'd de-morgan it. And then I'd invariably write `if not response not in…` or something stupid like that… – abarnert Dec 18 '13 at 02:07
2

If response is either n or s, both the conditions will be met. The best way to do this would be

if response in ('n', 's'):
    print ("it works")
else:
    print ("this works")
thefourtheye
  • 233,700
  • 52
  • 457
  • 497