Hi everyone,

I am new to Python, I am stuck with this problem.
"If your initially deposit $10000 into the account, and earns 6% interest per year, to meet your monthly expense, at the beginning each month you withdraw $500, how long the account will be depleted?
if you withdraw $10 per month, how long the account will be depleted?
how about $50? $100?
print the end balance and number of years the account will last.

Here is I've written so far:

print"This program calculate how long your account can last."
c = initial = float (raw_input ("Enter the initial in the account:"))
d = monthly_withdrawal = float (raw_input ("Enter the monthly withdrawal:"))
i = yearly_interest_rate = float (raw_input ("Enter yearly interest rate:"))
mrate = 1/1200

year = 0

shortfall = d - mrate*c

for i in range (year+1):
if shortfall <= 0:
print "The account will last forever !"
if shortfall > 0:
year = year + 1
balance = c - shortfall
print year, balance

but it does not work. can anybody help?

One thing to note is that you have to do the calculations over and over until the money is depleted.

One thing i would do is consider how i would ad interest. id they enter 6% that is the same as balance*0.06. That is how much interest is added to be added on.

So balance*1.06 = new balance after interest added on. This is a geometric series with the common ration r=1.06 (in this case) and n is unknown.

geometric series use the following formula

Un = ar**(n-1)

where r is the common ration, and a is the first term.
note you will also have to include the fact that x is taken of at the start of each month, which could be something like this not entrily sure don't have the time to sit and work it out right now but

Un = ar**(n-1) - x*(n-1)


This gives us to methods of solving this problem, one is to loop through and do each step and count how many months it takes. The second option is to use maths and logarithms to calculate the vale of n based of a geometric series. It is upto you how you wish to go about solving this problem.

hope this proves helpful
Chris

Suggestion: print out mrate after executing the statement mrate = 1/1200 . Then try mrate = 1./1200. instead.

Also, please use the [code=Python] and [/code] tags around your code so it comes out properly formatted.

Also, this will not work. You have two separate blocks of memory. I have reduced your code so it only tests the year variable. The value at memory block #2 never changes so that snippet only prints once, (i.e it is initialized as year[memory block #1]+1 = 0+1 at a separate location [memory block #2]). It is not a good idea to use "i", "l", "o" as single character variable names. They look too much like numbers.

year = 0     ## memory block #1
for i in range (year+1):     ## memory block #2
   if year < 5:
      year = year + 1        ## memory block #1
   print year                ## memory block #1
#
#
print "\n\n A while statement will work though since"
print 'there isn't a local variable like there is in the "range" statement'
year = 0                                                                        
ctr = 0                                                                         
while ctr < year+1:     
   if year < 5:
      year = year + 1                                                           
   print year                                                                   
   ctr += 1                                                                     
   if ctr > 1000:     ##  prevent infinite loop
      break

I have one more thing that I don't understand your code. why would you use " if year <5" ?

I have one more thing that I don't understand your code. why would you use " if year <5" ?

To stop the loop after 5 iterations, because 5 iterations is more than enough to make the point.

Check the amount of interest earned each month (amt * rate/12). If this is greater than what you're taking out of the account, then you'll never deplete the account, so there's no point in trying to see how long it will last, as you'll get caught in an infinite loop. Otherwise, the easiest thing to do is to loop through the months, calculating the interest and the new balance.

Here's a bit of code to get you started. You'll have to play with it to do things like validating input, etc.

init_amt = float(raw_input('Enter the initial amount, in $ '))
int_rate = float(raw_input('Enter the interest rate, in % '))
month_out  = float(raw_input('Enter the monthly withdrawl amount, in $ '))

month_int = init_amt * int_rate / 1200.0

if month_int >= month_out:
    print 'The monthly interest earned is greater than the monthly withdrawl,'
    print 'so the money will never run out'
else:
    print '   month\t interest\t  balance'
    curr_bal = init_amt
    mth_cnt = 0
    while curr_bal > 0:
        int_amt = curr_bal * int_rate / 1200.00
        curr_bal = curr_bal - month_out + int_amt
        mth_cnt += 1
        print ('%8i' + '\t' + '%8.2f' + '\t' + '%8.2f') % (mth_cnt, int_amt, curr_bal)
    print 'the money will run out after ' + str(mth_cnt) + ' months'

I'd suggest going one step further - input an inflation rate, and every 12 months, increase the monthly withdrawl amount by that rate. Of course, then you'll have to watch that test at the beginning, because eventually what comes out might exceed the interest, so your money won't necessarily last forever, at least not as simply as the formula I used would indicate.

This article has been dead for over six months. Start a new discussion instead.