How to fix a loop that runs once if value is correct 1st time. But runs for ever if the 1st value is wrong and 2nd value is correct.
<p>I'm doing this controlled assessment. I am just a beginner so I don't know too much about python.</p>

<p>I have this code:</p>

<pre><code># defining qualification
def qualification():
    print("\nQualification Level") # informs user what is AP + FQ
    print('\n"AP" = Apprentice', '\n"FQ" = Fully-Qulaified')

    user_qual = input("Enter your Qualification Level")

    # error message if any other data is entered
    while user_qual not in ("AP", "FQ"):
        print("You have entered one or more data wrong!")
        print("Please re-enter Qualification Level!")
        qualification()
</code></pre>

<p>Every time this code runs, it runs good until the while loop. If I enter the correct value (i.e. AP or FQ) the fist time I run the code, then the while loop doesn't run, as it should. But if I enter the wrong value the first time (any value that is not FQ or AP) the while loop runs as it should but then after it runs the first time, eve if I enter the correct value after entering the wrong value the while loop doesn't stop looping. A infinite loop is being created.</p>

<p>Please provide an answer, remember I'm  just a beginner at programming with python, so please don't let the solution be too complicated. </p>

<p>You tried to use recursion at the wrong place. </p>

<p>If the user input is wrong for the first time, you will get into a deeper level recursion that will (maybe) input the right input (or will go deeper).</p>

<p>But then, it will end and go back to the previous level of recursion, where the <code>user_qual</code> variable is still the same, which will result in infinite loop.</p>

<p><strong>Note: variables are not the same while running into a different recursion level. You are getting into another local scope.</strong> You might want to do a little search about scopes before you continue with your program.</p>

<hr>

<p>So, instead of calling to <code>qualification()</code> on the last line, just input again:</p>

<pre><code>while user_qual not in ("AP", "FQ"):
    print("You have entered one or more  data wrong!")
    user_qual = input("Please re-enter Qualification Level!")
</code></pre>

<hr>

<p>Another solution will be to use <code>global user_qual</code> in the beginning of the function and in the beginning of the loop. Read about global variables in python if you plan to act so.</p>

python python-3.x