Why are the conditions in if block not working correctly?
<p>Yakshemash ! I am a Java programmer learning python to make throwaway scripts. I want to make a parser which is shown in the code below.</p>

<pre><code>class Parser(object):
    def parse_message(self, message):
        size= len(message)
        if size != 3 or size != 5:
            raise ValueError("Message length is not valid.")

parser = Parser()
message = "12345"
parser.parse_message(message)
</code></pre>

<p>This code throws the error:</p>

<pre><code>Traceback (most recent call last):
  File "/temp/file.py", line 9, in &lt;module&gt;
    parser.parse_message(message)
  File "/temp/file.py", line 5, in parse_message
    raise ValueError("Message length is not valid.")
ValueError: Message length is not valid.
</code></pre>

<p>What is my mistake and how do I correct it ?</p>

<p>Syntactically, there is nothing wrong in your code. The output is correct according to your code.</p>

<pre><code>size != 3 or size != 5:   
# This will be always *true* because it is impossible for **message**
  to be of two different lengths at the same time to make the condition false.
</code></pre>

<p>Since the above condition always results <em>true</em>, I assume that you wanted to do something else. </p>

<p>Now putting an <strong>and</strong> logical operator works like below:</p>

<pre><code>size != 3 and size != 5:  
# This will be true if the length of **message** is neither 3 nor 5
# This will be false if the length of **message** is either 3 or 5
</code></pre>

python