简体   繁体   中英

How can I find out the number of outputs in a loop?

I am a beginner at python and I'm struggling with one of my (simple) college assignments. I have been given the following instructions:

A bank is offering a savings account where a yearly fee is charged. Write a program that lets the user enter

  1. An initial investment.

  2. The yearly interest rate in percent.

  3. The yearly fee.

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

An example run of the program:

Enter the investment: 1000

Enter the interest rate: 10

Enter the fee: 10

The investment doubles after 7 years.

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!:

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<=(2*p):
    f=(float(f*((1+i)**t)-m)
    t=t+1

print("The investment doubles after",t,"years")

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. NOTE You should use input instead of raw_input in my example if using Python 3+. I use 2.7 so I use raw_input.

#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 >= 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) >= (years * (investment * (interest / 100))):
            print('Annual Fee Exceeds Interest - Losing Money')
            break
#initial call of our main function/begins loop
main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM