0

I was trying to debug some Python code of mine and I can't seem to figure this out. Any ideas why this keeps repeating if I input the correct argument for the direction input variable?

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
while direction != "encode" or direction != "encrypt" or direction != "decrypt" or direction != "decode":
    print("Please put in a valid direction!\n")
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
3
  • 4
    Try and instead of or. Commented May 11, 2022 at 5:51
  • 1
    Every possible string is either not "encode" or not "encrypt" Commented May 11, 2022 at 5:53
  • en.wikipedia.org/wiki/De_Morgan%27s_laws Commented May 11, 2022 at 6:30

3 Answers 3

2

Try and instead of or. Alternatively, you might find the following more readable:

while direction not in ('encode', 'encrypt', 'decrypt', 'decode'):
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Learning here as I go. Appreciate the help. That is much clearer now. Thanks everyone!
0

Try this:

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")

method = ["encode", "encrypt", "decrypt", "decode"]

while direction not in method:
    print("Please put in a valid direction!\n")
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")

Comments

0

Your condition for while loop is not correct. for example when you enter "decode" as input your encode != true is correct so loops continue
You can use this code:

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
while direction not in {"encode", "encrypt", "decode", "decrypt"} :
    print("Please put in a valid direction:!")
    direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.