While statment loop in python
<p>I am learning python, and I am stuck in an infinite while loop in the following code.</p>

<pre><code>A = input("Hello what is your name? ")
D = input("What is today's date? ")
B = input("Did you have a good day [y/N]")
while B != "y" or B != "Y" or B != "N" or B != "n":
    B = input("Did you have a good day [y/N]")
else:
    if B == "Y" or B == "y":
        print(A + ", ")
        C = input("Tell me about your day ")
        with open("Start.txt", "a") as infile:
            infile.write("\n")
            infile.write(A)
            infile.write(" ran Start.py and said he had a good day on ")
            infile.write(D)
            infile.write(".")
            infile.write("\n    He reports today:\n        ")
            infile.write(C)
    elif B == "N" or "n":
        print(A + ", ")
        C = input("Tell me about your day ")
        with open("Start.txt", "a") as infile:
            infile.write("\n")
            infile.write(A)
            infile.write(" ran Start.py and said he had a bad day on ")
            infile.write(D)
            infile.write(".")
            infile.write("\n    He reports today:\n        ")
            infile.write(C)
</code></pre>

<p>The problem happens when B is compared to see if it is equal to Y, y, N, or n
but it still no matter what input I give it for B sticks me into the while statement and keeps me there.</p>

<p>The problem is here:</p>

<pre><code>while B != "y" or B != "Y" or B != "N" or B != "n":
</code></pre>

<p><code>B</code> is <em>always</em> not equal to one of those. If it's "y" it is not equal to the other three, and so on. And since you are using <code>or</code> to combine these conditions, the loop continues if any one is true, which, as we've seen, is always the case.</p>

<p>Rewrite using <code>and</code>:</p>

<pre><code>while B != "y" and B != "Y" and B != "N" and B != "n":
</code></pre>

<p>Or apply DeMorgan's law (factor the <code>not</code> out of the expression and swap <code>and</code>/<code>or</code>):</p>

<pre><code>while not (B == "y" or B == "Y" or B == "N" or B == "n"):
</code></pre>

<p>Or best of all, write it the Python way:</p>

<pre><code>while B.lower() not in "yn":
</code></pre>

<p>(This also accepts an empty answer, which according to your prompt is equivalent to "N". To handle this, just convert your <code>elif</code> to a plain <code>else</code>, without a condition.)</p>

python while-loop