•
•
•
•
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
![]() |
•
•
Join Date: May 2008
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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?
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?
•
•
Join Date: Apr 2008
Posts: 42
Reputation:
Rep Power: 1
Solved Threads: 4
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
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
•
•
Join Date: Dec 2006
Posts: 408
Reputation:
Rep Power: 2
Solved Threads: 56
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.
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!
Here's a bit of code to get you started. You'll have to play with it to do things like validating input, etc.
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.
Python Syntax (Toggle Plain Text)
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.
Last edited by kdoiron : May 22nd, 2008 at 10:32 am.
But I don't like SPAM!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- How can someone hack your AOL account? (Geeks' Lounge)
- slow connection to hotmail account (Windows NT / 2000 / XP / 2003)
- Hotmail ONLY accessing page problem -- Only with one account (Web Browsers)
- bank account (C++)
- Do u use your root account on a regular basis? (*nix Software)
Other Threads in the Python Forum
- Previous Thread: Writing arrays to a column of a spreadsheet?
- Next Thread: How to run a program


Linear Mode