Yes or No answer from user with Validation and restart option?
<p>(py) At the moment, the code below does not validate/output error messages when the user inputs something other than the two choices "y" and "n" because it's in a while loop. </p>

<pre><code>again2=input("Would you like to calculate another GTIN-8 code? Type 'y' for Yes and 'n' for No. ").lower() #**

    while again2 == "y":
        print("\nOK! Thanks for using this GTIN-8 calculator!\n\n")
        restart2()
        break                                                                                                                    #Break ends the while loop

restart2()
</code></pre>

<p>I'm struggling to think of ways that will allow me to respond with an output when they input neither of the choices given. For example:</p>

<pre><code>if again2 != "y" or "n"
   print("Not a valid choice, try again")
   #Here would be a statement that sends the program back to the line labelled with a **
</code></pre>

<p>So, when the user's input is not equal to "y" or "n" the program would return to the initial statement and ask the user to input again. Any ideas that still supports an efficient code with as little lines as possible? Thanks!</p>

<p>A recursive solution:</p>

<pre><code>def get_input():
    ans = input('Y/N? ') #Use raw_input in python2
    if ans.lower() in ('y', 'n'):
        return ans
    else:
        print('Please try again.')
        return get_input()
</code></pre>

<p>If they're really stubborn this will fail when it reaches maximum recursion depth (~900 wrong answers)</p>

python validation loops while-loop restart