If statement with input check with incorrect outputs
<p>Backstory: I have been trying to actually learn python instead of just snipping from others. I have created a simple script that uses <code>webbrowser</code>. It may be a dirty script and I would love input like "you should do <em>this</em>", "<em>this</em> can be simplified". The thing i cant figure out is using <code>if</code> statement to handle incorrect input, prompt, then recheck the if statement. I tried searching but nothing assisted in this.</p>

<pre><code>import webbrowser
a = input ('Do you want to search?(y or n)')
if a == ('y' or 'yes' or 'Y' or 'Yes' or 'YES'):
    b = input ('What do you want to search?')
    ab = ('https://www.google.com//search?q='+b)
    urlab = ab
    webbrowser.open(urlab)

else:
    x = input('Where do you want to go?: ')
    new = 2 # open in a new tab, if possible
    # open a public URL, in this case, the webbrowser docs
    url = x
    webbrowser.open(url)
</code></pre>

<p>The question is: How do i ether do a recurring that will handle incorrect answers. If they use something other then the listed yes, it will print <code>please use ('y' or 'yes' or 'Y' or 'Yes' or 'YES')</code>, then prompt again and allow for input. I know i will have to change it to a nested if statement to allow the same with no to move to next. Also as is, when i use the code and enter <code>'y'</code> it will open with my default (firefox), but if i enter anything else, it only opens in IE without the google search but "searching" like <code>http://fun/</code> instead of <code>https://www.google.com//search?q=fun</code> as it should.<br>
What did leave out? Also if you could post information on in-depth the meaning behind the code to help further learning. Thank you all!</p>

<p>There's a different way to check if the value is one of the given values. You should make your if condition be like this:</p>

<pre><code>if a in ('y', 'yes', 'Y', 'Yes', 'YES'):
</code></pre>

<p>It is fast and understandable.  </p>

<p>Here's a reason why your condition does not work.<br>
Let's say we entered <code>'no'</code>. Let's see what happens in your if statement:<br>
1. First, the <code>('y' or 'yes' or 'Y' or 'Yes' or 'YES')</code> is evaluated. Since a non-empty string in Python converts to <code>True</code>, this part evaluates <strong>entirely</strong> to <code>True</code><br>
2. Then, the comparison takes place. It looks like this:</p>

<pre><code>if a == True:
</code></pre>

<p>With <code>a</code> being a string <code>'no'</code>. That's obviously not what you want. Use the method a described above and everything will be fine</p>

<p>To constantly re-ask until a correct input is received, try an endless loop with a <code>break</code> statement:</p>

<pre><code>while True:
    a = input()
    if a in ...:
        # process input
        break
    else:
        print("please use 'y' or 'yes' or 'Y' or 'Yes' or 'YES'")
</code></pre>

python if-statement python-3.4