2

Problem: I'm trying to invoke ask_user() again if the user inputs words != 'encrypt' or 'decrypt', but the same error message appears when the input IS correct.

def ask_user():
    while True:
        mode = input("Would you like to encrypt or decrypt:   ")
        if mode != 'encrypt' or mode != 'decrypt':          
            print("Please retry; you must type lowercase. \n")
            ask_user()

        else:
            message = input("Enter your message:   ")

It seems that using more than one != statements on same line doesn't work as I'd imagined:

# test to support above observ.
for n in range(4):
    if n != 2 or n != 3:
        print('me')
    else:
        print(n)

How should I change the code to fix this problem?

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
mikerover
  • 152
  • 2
  • 11
  • 1
    `mode not in ('encrypt', 'decrypt')` – Dan D. Mar 24 '19 at 17:59
  • https://en.wikipedia.org/wiki/De_Morgan%27s_laws – Patrick Artner Mar 24 '19 at 18:00
  • the problem is, if `n` is actually provided the value 2, the `n!=3` becomes true! and if `n` is provided value 3, `n!=2` becomes true! it is impossible to make the two conditions result in a false together! (tl;dr with negation, you really need to think of how the conditionals behave with `or`) – Paritosh Singh Mar 24 '19 at 18:00
  • Possible duplicate of [How to have multiple conditions for one if statement in python](https://stackoverflow.com/questions/36757965/how-to-have-multiple-conditions-for-one-if-statement-in-python) – tripleee Mar 24 '19 at 18:00
  • Okay, seriously, do we have a duplicate for these kinds of questions? – Aran-Fey Mar 24 '19 at 18:01
  • 2
    @tripleee I don't think that answers the question. The OP is clearly confused about boolean logic; they're not *really* asking how to implement multiple conditions. – Aran-Fey Mar 24 '19 at 18:07
  • @Aran-Fey There's gotta be one somewhere. This is a common question. – miike3459 Mar 24 '19 at 18:13
  • Possible duplicate of https://stackoverflow.com/questions/6838238/comparing-a-string-to-multiple-items-in-python – tripleee Mar 24 '19 at 18:15
  • 1
    @ tripleee oh come on, I'm not asking for an "efficient" solution involving for loops - which does not seem v efficient to me here... – mikerover Mar 24 '19 at 18:23
  • [Python program taking two integer inputs to find even and odd](https://stackoverflow.com/questions/52063802/python-program-taking-two-integer-inputs-to-find-even-and-odd/52064012#52064012) is similar, but I wouldn't call it a duplicate. – Trevor Reid Mar 24 '19 at 18:33

3 Answers3

2

n != 2 or n != 3 will always be true. If n is 2 then it's not 3. All other values are not 2.

You intended n != 2 and n != 3.

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
2

Your problem is that you are using or instead of and. If you think about how the code is interpreted:

Let's say that mode="encrypt". Step by step:

mode != 'encrypt' evaluates to false. All good so far.

mode != 'decrypt', however, evaluates to true. This is a problem.

The final expression sent to the if will be: false or true. This, finally, evaluates to true, causing the if block to be entered.

Changing it to and means that both of invalid modes will have to be checked true for the block to be entered.

Luke B
  • 2,075
  • 2
  • 18
  • 26
0

You need to use and, not or. Because n will never be equal to both 3 and 4, the if statement that incorporates or will always resolve to True

Alec
  • 8,529
  • 8
  • 37
  • 63