booleans... I don't see what I am missing here... I am trying to write a very basic text adventure, but I am missing something basic... When I put eg: next = "go west" next == "go west" it gets True BUT next == "w" or "go west" it gets 'go west' next == ("w" or "go west") it gets False What am I not understanding about booleans?
have you tried next == "w" or next == "go west"
okay cool that works... I don't really see why next == (x or y or z) shouldn't work, but okay, that solves my problem anyhow :) Thanks
next == (some boolean operation) is actually analyzing: next == result_of_boolean_operation. Always remember PEMDAS -> parenthesis have a higher hierarchy in an expression. What is actually happening is that Python is checking whether or not next == True, which is false.
still... then next == (True or False) I just would have thought that would have given True... as opposed to next == (True and False) which would be False... Well, so I thought... wrongly I guess. On the so called truth tables True or False is True... Eh, I'll just note it and maybe I'll understand after some practice :)
Nope, next == True false, because next has no value whatsoever, or has no boolean value. Albeit, I take it was no value, because Python has boolean values for nonempty variables (like an int = 0 is False, int != 0 is True, even though is not really used in Python, it's somewhat of a bad code - less readability).
But even if it's nonempty, I don't think you can compare ints/strings to boolean values in Python, only some sort of "boolean operator", like if next: (for lack of better name, I am unsure about the correct nomenclature).
when you did: next == "w" or "go west" Python interprets this as: (next == "w") or "go west" which translates to: False or "go west" and, in an "or" statement, it always returns the value of the first True value, which happens to be "go west". so it returns the string "go west". think of the following statements: myString=None if myString: print "None=",myString myString="" if myString: print "empty string=",myString myString="go west" if myString: print "non-empty string=",myString if you run this, the only print statement that will be executed is the last one. similarly: print 1 or 2 or 3 will print 1 print "" or 42 will print 42 print "w" or "go west" will print "w" so your last statement: next == ("w" or "go west") is translated to: next == "w" #since "w" or "go west" translates to just "w") which is False hope this clarifies the behaviour.
Join our real-time social learning platform and learn together with your friends!