944,080 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2795
  • Python RSS
Oct 29th, 2006
0

Python - Calculate no of years to double

Expand Post »
HI,

I have written a small program that uses a while loop to calculate how many years it takes for an investment to double given an interest rate:

However, I think there is something minor wrong with the calculation for years, since when testing I put in 100% it should say 1 year, yet says it takes 2 years.

Python Syntax (Toggle Plain Text)
  1. def main():
  2. # Introduction
  3. print __doc__
  4.  
  5. presVal = 1 # Set present value to $1
  6. intRate = input("Enter the Interest Rate >>") # Ask for the Interest Rate
  7. years = 0 # Start the years at zero
  8. futVal = presVal # Set the variable used to accumulate
  9.  
  10. # Set the while loop
  11. while futVal <= presVal *2: # Continue 'while' futVal has not doubled
  12. futVal = futVal + futVal *((float(intRate)/100)) # Calc the next loop
  13. years = years + 1 # Add 1 to the years
  14.  
  15. # Calculate the amount left of the part year
  16. fut = futVal%2
  17.  
  18. # Print the answer
  19. print
  20. print "It takes approximately %0.2f years to double the present value" % (years-fut)

Any help would be most welcomed.
Last edited by vegaseat; Apr 29th, 2011 at 4:37 pm. Reason: changed old PHP code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 29th, 2006
0

Re: Python - Calculate no of years to double

[php]while futVal <= presVal *2[/php]

should be

Python Syntax (Toggle Plain Text)
  1. while futVal < presVal *2

Right now, when the value is doubled, it still executes the loop once more.

With this change, you get an answer of "1.00 years."
Last edited by vegaseat; Apr 29th, 2011 at 4:39 pm. Reason: oldcode tags
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 29th, 2006
0

Re: Python - Calculate no of years to double

Thank you. Stupid mistake I guess...
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Apr 27th, 2011
0
Re: Python - Calculate no of years to double
Simpler program:

you should type line by line so the indents come into it
Python Syntax (Toggle Plain Text)
  1. #A program to determine how long it takes for an investment to double
  2. def main():
  3. I=input("What is the annualized interest rate as a decimal?: ")
  4. i=0
  5. z=1
  6. while z<2:
  7. i=i+1
  8. z=(z*(1+I))
  9. if z>=2:
  10. print i
  11. main()
Last edited by Narue; Apr 27th, 2011 at 4:56 pm. Reason: Added code tags
Reputation Points: 10
Solved Threads: 1
Newbie Poster
NoddyNUIG is offline Offline
1 posts
since Apr 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: error debugging
Next Thread in Python Forum Timeline: Python "is" statement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC