User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 402,620 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,292 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 330 | Replies: 8
Reply
Join Date: May 2008
Posts: 5
Reputation: ivy47469 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ivy47469 ivy47469 is offline Offline
Newbie Poster

How long the account can last

  #1  
May 20th, 2008
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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2008
Posts: 42
Reputation: Freaky_Chris is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
Freaky_Chris Freaky_Chris is offline Offline
Light Poster

Re: How long the account can last

  #2  
May 20th, 2008
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
Last edited by Freaky_Chris : May 20th, 2008 at 12:36 pm. Reason: to remove a * so that it isn't x**(n-1) ause thats just wrong
Reply With Quote  
Join Date: May 2007
Posts: 255
Reputation: BearofNH is on a distinguished road 
Rep Power: 2
Solved Threads: 18
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz in Training

Re: How long the account can last

  #3  
May 20th, 2008
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.
Reply With Quote  
Join Date: Apr 2008
Posts: 42
Reputation: Freaky_Chris is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
Freaky_Chris Freaky_Chris is offline Offline
Light Poster

Re: How long the account can last

  #4  
May 20th, 2008
ok ignore me ranting on about geometric series, the fact you are withdrawing x throws it out.

Chris
Reply With Quote  
Join Date: Dec 2006
Posts: 408
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 56
woooee woooee is offline Offline
Posting Pro in Training

Re: How long the account can last

  #5  
May 20th, 2008
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
Last edited by woooee : May 20th, 2008 at 4:36 pm.
Reply With Quote  
Join Date: May 2008
Posts: 5
Reputation: ivy47469 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ivy47469 ivy47469 is offline Offline
Newbie Poster

Re: How long the account can last

  #6  
May 20th, 2008
I have one more thing that I don't understand your code. why would you use " if year <5" ?
Reply With Quote  
Join Date: Dec 2006
Posts: 408
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 56
woooee woooee is offline Offline
Posting Pro in Training

Re: How long the account can last

  #7  
May 20th, 2008
Originally Posted by ivy47469 View Post
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.
Reply With Quote  
Join Date: May 2008
Location: Toronto
Posts: 37
Reputation: kdoiron is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kdoiron's Avatar
kdoiron kdoiron is offline Offline
Light Poster

Re: How long the account can last

  #8  
May 22nd, 2008
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.
But I don't like SPAM!
Reply With Quote  
Join Date: May 2008
Location: Toronto
Posts: 37
Reputation: kdoiron is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
kdoiron's Avatar
kdoiron kdoiron is offline Offline
Light Poster

Re: How long the account can last

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

  1. init_amt = float(raw_input('Enter the initial amount, in $ '))
  2. int_rate = float(raw_input('Enter the interest rate, in % '))
  3. month_out = float(raw_input('Enter the monthly withdrawl amount, in $ '))
  4.  
  5. month_int = init_amt * int_rate / 1200.0
  6.  
  7. if month_int >= month_out:
  8. print 'The monthly interest earned is greater than the monthly withdrawl,'
  9. print 'so the money will never run out'
  10. else:
  11. print ' month\t interest\t balance'
  12. curr_bal = init_amt
  13. mth_cnt = 0
  14. while curr_bal > 0:
  15. int_amt = curr_bal * int_rate / 1200.00
  16. curr_bal = curr_bal - month_out + int_amt
  17. mth_cnt += 1
  18. print ('%8i' + '\t' + '%8.2f' + '\t' + '%8.2f') % (mth_cnt, int_amt, curr_bal)
  19. 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.
Last edited by kdoiron : May 22nd, 2008 at 10:32 am.
But I don't like SPAM!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 1:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC