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>Your problem is straightforward. You have to use <a href="https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops" rel="nofollow"><code>continue</code></a> or <a href="https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops" rel="nofollow"><code>break</code></a> wisely. These are called control flow statements. They control the normal flow of execution of your program. So <code>continue</code> will jump to the top-most line in your loop and <code>break</code> will simply jump <em>out</em> of your loop completely, be it a <code>for</code> or <code>while</code> loop. </p>

<p>Going back to your code: </p>

<blockquote>
  <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.</p>
</blockquote>

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

<p>This way, you jump back (you continue) in your loop to the first line:  <code>import math</code>. Doing imports inside a loop isn't useful this way the import statement is useless as your imported module is already in <code>sys.modules</code> list as such the <code>import</code> statement won't bother to import it; if you're using <code>reload</code> from <code>imp</code> in 3.X or available in <code>__builtin__</code> in Python 2.x, then this might sound more reasonable here. </p>

<p><em>Ditto:</em> </p>

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

<p>To state this snippet in English: if <code>ans</code> is equal to <code>"n"</code> or <code>"no"</code> then break the loop; else if <code>ans</code> is equal to <code>"y"</code> or <code>"yes"</code> then continue. If nothing of that happened, then (<code>else</code>) <code>continue</code>. The nested <code>while</code> loop isn't needed.</p>

<blockquote>
  <p>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? </p>
</blockquote>

<p>Not sure if I understood your question, you can take input three times in different ways: </p>

<pre><code>for line in range(3):
    in = input("Enter text for lineno", line)
    ...do something...
</code></pre>

python