0
variable1 = 0
while variable1 != "1" or variable1 != "2" or variable1 != "3":
    variable1 = input ("Enter variable1: ")
print("Succes")

My code never goes out of the while loop even if the variable is assigned 1 or 2 or 3. Am I missing something here or doing something wrong? I never read any documentation about Python that said or statements wouldn't work in while loops. According to propositional calculus this should be correct because True or False or False = True

I am aware that I did not use integers by the way.

Thanks in advance!

6
  • 2
    what's the != to doing there? <ominous chanting> Kill the to! Kill the to! Kill the to! </ominous chanting> Commented Jan 20, 2014 at 21:56
  • What happens when variable1 = 'al;dfjakl;fkja;lkajdfj'? Commented Jan 20, 2014 at 21:56
  • variable1 != to "1" is a syntax error. Why is the "to" in there? Commented Jan 20, 2014 at 21:57
  • @inspectorG4dget That's not supposed to be in there, a strange typo I made. I'll fix it. Commented Jan 20, 2014 at 21:57
  • @Joe Then it will be false and not work, asking for input again. Commented Jan 20, 2014 at 21:58

4 Answers 4

5

The condition of your while-loop will always evaluate to True because variable1 will always be not equal to "1" or not equal to "2".

Instead, you will want to use not in here:

variable1 = 0
while variable1 not in ("1", "2", "3"):
    varible1 = input("Enter variable1: ")
print("Succes")

However, judging by your code structure, I think that you want variable1 to be an integer, not a string.

If so, then you can use this on Python 3.x:

variable1 = 0
while variable1 not in (1, 2, 3):
    varible1 = int(input("Enter variable1: "))
print("Succes")

or, if you are on Python 2.x, you can use this:

variable1 = 0
while variable1 not in (1, 2, 3):
    varible1 = int(raw_input("Enter variable1: "))
print "Succes"
Sign up to request clarification or add additional context in comments.

7 Comments

Would be better to use a set instead of a tuple, to optimize lookup time
I run it and never stops, shouldn't be (1,2,3)
@xndrme - Judging by his print syntax as well as the fact that he is comparing with strings to begin with, I am assuming that the OP is on Python 3.x. If so, input will return a string object.
@iCodez Ok, I'm in 2.7, my mistake :(
@inspectorG4dget: On modern Python 3's CPython reference interpreter, the set will perform better, but prior to 3.2, it would have performed worse (because it rebuilt the set prior to each test). It was only optimized to make such tests against a set literal of constant literals build a frozenset at compile time and reuse it in 3.2. The set would always lose prior to 3.2 though, since building the set would already be O(n), so you didn't save anything over a direct scan of a tuple (which was always a constant).
|
4

The condition of your while loop will ALWAYS be true. In order for it to be false, variable1 must equal "1", "2", and "3", which is impossible for a single string.

>>> variable1 == "1"
>>>
>>> variable1 != "1"
False
>>> variable1 != "2"
True
>>> variable1 != "3"
True
>>> False or True or True
True # So the loop will continue execution

Do you want your while loop to exit if variable1 equals "1", "2", or "3"?

while not (variable1 == "1" or variable1 == "2" or variable1 == "3"):

If variable1 equals either "1", "2", or "3", then it's useful to imagine how the condition will be resolved:

while not (True or False or False):

while not (True):

while False: # Exit

1 Comment

How would I go around changing this around so that it will work? Thank you for your answer!
1
  1. You need to update the variable in the while loop
  2. If you use input you need to compare as int

variable1 = 0
while variable1 not in {1,2,3}:
    variable1 = input("Enter variable1: ")

print("Succes")

1 Comment

Would be better to use a set instead of a list, to optimize lookup time
0

Instead of 'or', you should use 'and'. You want to keep asking for input as long as the input is not a "1" AND it is not a "2" AND it is not a "3".

1 Comment

I'm not a Python programmer (yet). While my answer is logically valid, the solution offered by @xndrme is more elegant.

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.