Interest Calculator looping issue Python
<p>beginner here. I've made an interest calculator program to help me with my loans of different sorts. I'm having two issues to finalize my program. Here's the program. I tried looking up the problems but I wasn't sure how to word it so I thought I'd just ask a question in total.</p>

<pre><code>x=1
while x==1:
    import math
    loan=input("Enter Loan amount: ")
    rate=input("Enter rate: ")
    if rate.isalpha() or loan.isalpha():
        print("Hey that's not a number!")
        break
    rate=rate.replace("%","")
    loan=float(loan)
    rate=float(rate)*0.01
    amount1y=round(loan*(math.e**(rate*1)),2)
    amount5y=round(loan*(math.e**(rate*5)),2)
    amount10y=round(loan*(math.e**(rate*10)),2)
    monthlypay=round(amount1y-loan,2)
    print("Year 1 without pay: " + str(amount1y))
    print("Year 5 without pay: " + str(amount5y))
    print("Year 10 without pay: " + str(amount10y))
    print("Amount to pay per year: " + str(monthlypay))
    print("Want to do another? Y/N?")
    ans=input('')
    ans=ans.lower()
    y=True
    while y==True:
        if ans=="n" or ans=="no":
            x=0
            break
        elif ans=="y" or ans=="yes":
            y=False
        else:
            print("You gotta tell me Yes or No fam...")
            print("I'll just assume that mean's yes.")
            break
</code></pre>

<p>My issue is in two locations. First during the</p>

<pre><code>if rate.isalpha() or loan.isalpha():
        print("Hey that's not a number!")
        break
</code></pre>

<p>How do I write this so that instead of it ending the program all together, it instead restarts from the top until they put in a number. Also as a side just for fun and knowledge. Lets say they enter text three times in a row, and at that point it just executes the program how would I go about doing that also?
Finally during this part of the code:</p>

<pre><code> while y==True:
        if ans=="n" or ans=="no":
            x=0
            break
        elif ans=="y" or ans=="yes":
            y=False
        else:
            print("You gotta tell me Yes or No fam...")
            print("I'll just assume that mean's yes.")
            break
</code></pre>

<p>the else at the end, without that break, will continue printing "You gotta tell me Yes or No fam..." forever. How do I make it so that instead of breaking the while statement, it'll just restart the while statement asking the question again?</p>

<p>Thanks for your help!
P.S. This is python 3.4.2</p>

<p>On both of your examples, I believe your looking for Python's <code>continue</code> <a href="https://docs.python.org/3/reference/simple_stmts.html#continue" rel="nofollow">statement</a>. From the Python Docs:</p>

<p>(emphasis mine)</p>

<blockquote>
  <p>continue may only occur syntactically nested in a for or while loop, but not nested in a function or class definition or finally clause within that loop. <strong>It continues with the next cycle of the nearest enclosing loop.</strong>
  When continue passes control out of a try statement with a finally clause, that finally clause is executed before really starting the next loop cycle.</p>
</blockquote>

<p>This basically means it will "restart" your <code>for/while</code>-loop.</p>

<hr>

<p>To address your side note of breaking the loop if they get the input wrong after three tries, use a counter variable. increment the counter variable each time the user provides the wrong input, and then check and see if the counter variable is greater than 3. Example:</p>

<pre><code>counter = 0
running = True:
while running:
    i = input("Enter number: ")
    if i.isalpha():
        print("Invalid input")
        counter+=1
    if counter &gt;= 3:
        print("Exiting loop")
        break
</code></pre>

<p><strong>Unrelated notes:</strong></p>

<ul>
<li>Why not use a boolean value for <code>x</code> as well?</li>
<li>I usually recommend putting any imports at the module level for the structure an readability of one's program.</li>
</ul>

python