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

1
  • 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 first to see how easy it can be, then trying to figure out how to make it that easy in Python, maybe be helpful. Commented Dec 18, 2013 at 2:05

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

5 Comments

@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 I totally agree :)
@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 -- 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: 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…
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")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.