How can I find out the number of outputs in a loop?
<p>I am a beginner at python and I'm struggling with one of my (simple) college assignments. I have been given the following instructions:</p>

<p><strong>A bank is offering a savings account where a yearly fee is charged. Write
a program that lets the user enter</strong></p>

<ol>
<li><p>An initial investment.</p></li>
<li><p>The yearly interest rate in percent.</p></li>
<li><p>The yearly fee.</p></li>
</ol>

<p>the program should then calculate the time it takes
for the investment to double. The interest is added on once per year.</p>

<p>An example run of the program:</p>

<p>Enter the investment: 1000</p>

<p>Enter the interest rate: 10</p>

<p>Enter the fee: 10</p>

<p>The investment doubles after 7 years.</p>

<p>I have formulated the following code but am receiving an error message with regards to t. I would really appreciate if I could get some help, thanks!:</p>

<pre><code>t=0
p=float(input("Enter the investment:"))

a=float(input("Enter the interest rate:"))

m=float(input("Enter the fee:"))

i=(float(a/100))
f=p
while f&lt;=(2*p):
    f=(float(f*((1+i)**t)-m)
    t=t+1

print("The investment doubles after",t,"years")
</code></pre>

<p>I tried to write this in a way that was very easy to follow and understand. I edited it with comments to explain what is happening line by line. I would recommend using more descriptive variables. t/p/a/m/f may make a lot of sense to you, but going back to this program 6 months from now, you may have issues trying to understand what you were trying to accomplish. <em>NOTE</em> You should use input instead of raw_input in my example if using Python 3+. I use 2.7 so I use raw_input.</p>

<pre><code>#first we define our main function
def main():
    #investment is a variable equal to user input. it is converted to a float so that the user may enter numbers with decimal places for cents
    investment = float(raw_input("Starting Investment:  "))
    #interest is the variable for interest rate. it is entered as a percentage so 5.5 would equate to 5.5%
    interest = float(raw_input("Interest Rate as %, ex: 5.5  "))
    #annual_fee is a variable that will hold the value for the annual fee.
    annual_fee = float(raw_input("Annual Fee:  "))
    #years is a variable that we will use with a while loop, adding 1 to each year (but we wait until within the loop to do this)
    years = 1
    #we use a while loop as opposed to a for loop because we do not know how many times we will have to iterate through this loop to achieve a result. while true is always true, so this segment is going to run without conditions
    while True:
        #this is a variable that holds the value of our total money per year, this is equal to the initial investment + investment * interest percentage - our annual fee per year
        #I actually had to try a few different things to get this to work, a regular expression may have been more suited to achieve an interest % that would be easier to work with. do some research on regular expressions in python as you will sooner or later need it.
        total_per_year = investment + (years * (investment * (interest / 100))) - (annual_fee * years)
        #now we start adding 1 to our years variable, since this is a while loop, this will recalculate the value of total_per_year variable
        years += 1
        #the conditional statement for when our total_per_year becomes equal to double our initial investment
        if total_per_year &gt;= 2 * investment:
            #print years value (at time condition is met, so it will be 5 if it takes 5 years) and the string ' Years to Double Investment'
            print years,' Years to Double Investment'
            #prints 'You will have $' string and then the value our variable total_per_year
            print 'You will have $', total_per_year
            #this will break our while loop so that it does not run endlessly
            break
        #here is error handling for if the fee is larger than investment + interest
        if (years * annual_fee) &gt;= (years * (investment * (interest / 100))):
            print('Annual Fee Exceeds Interest - Losing Money')
            break
#initial call of our main function/begins loop
main()
</code></pre>

python loops while-loop