Python - Calculate no of years to double

Reply

Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Python - Calculate no of years to double

 
0
  #1
Oct 29th, 2006
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.

[PHP]def main():
# Introduction
print __doc__

presVal = 1 # Set present value to $1
intRate = input("Enter the Interest Rate >>") # Ask for the Interest Rate
years = 0 # Start the years at zero
futVal = presVal # Set the variable used to accumulate

# Set the while loop
while futVal <= presVal *2: # Continue 'while' futVal has not doubled
futVal = futVal + futVal *((float(intRate)/100)) # Calc the next loop
years = years + 1 # Add 1 to the years

# Calculate the amount left of the part year
fut = futVal%2

# Print the answer
print
print "It takes approximately %0.2f years to double the present value" % (years-fut)[/PHP]

Any help would be most welcomed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Python - Calculate no of years to double

 
0
  #2
Oct 29th, 2006
[php]while futVal <= presVal *2[/php]

should be

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

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."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Re: Python - Calculate no of years to double

 
0
  #3
Oct 29th, 2006
Thank you. Stupid mistake I guess...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC