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>The following code should work:</p>

<pre><code>import webbrowser
a = 'y'
while a != 'n':
    a = input ('Do you want to search?(y or n)')
    a = a[0].lower()
    if a in  "y":
        b = input ('What do you want to search?')
        ab = ('https://www.google.com//search?q='+b)
        urlab = ab
        webbrowser.open(urlab)

    elif a not in "n":
        print "Please only enter 'y' or 'n'."
</code></pre>

<p>The <strong><em>while</em></strong> loop tells python to loop as long as the answer is not "n".</p>

<p>The <strong><em>a = a[0]</em></strong> tells python to only use the first letter of the response.  This is to make the comparison easier later on.</p>

<p>The <strong><em>.lower()</em></strong> code tells python to convert the result to lowercase.  Again, this is to make the comparison easier later on.</p>

<p>Now our answer will always be lowercase, and the first letter entered.  So y ,yes, Yes, YES and n, no, No, NO will be converted to y or n.  Of course, any other text will be treated the same way (lowercase and first character), but we only care about y and n.</p>

<p>The rest should be pretty straightforward.  Since we have limited the possibilities of what the answer can be, we can do a simple comparison.</p>

<p>Below are modifications to check for only yes and no:</p>

<pre><code>import webbrowser
a = 'y'
while a != 'n':
    a = input ('Do you want to search?(y or n)')
    a = a.lower()
    if a in  ("y", "yes"):
        b = input ('What do you want to search?')
        ab = ('https://www.google.com//search?q='+b)
        urlab = ab
        webbrowser.open(urlab)
    elif a in "no"
        a = "n"
    elif a not in ("n", "no"):
        print "Please only enter 'y' or 'n'."
</code></pre>

python if-statement python-3.4