“or” conditional in Python troubles [duplicate]

Boolean expressions in most programming languages don’t follow the same grammar rules as English. You have to do separate comparisons with each string, and connect them with or:

if x == "monkey" or x == "monkeys":
    print "You're right, they are awesome!!"
else:
    print "I'm sorry, you're incorrect.", x[0].upper() + x[1:], "is not the right animal."

You don’t need to do the test for the incorrect case, just use else. But if you did, it would be:

elif x != "monkey" and x != "monkeys"

Do you remember learning about deMorgan’s Laws in logic class? They explain how to invert a conjunction or disjunction.

Leave a Comment