| | |
Python - Calculate no of years to double
![]() |
•
•
Join Date: Oct 2006
Posts: 36
Reputation:
Solved Threads: 0
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.
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 "It takes approximately %0.2f years to double the present value" % (years-fut)[/PHP]
Any help would be most welcomed.
![]() |
Similar Threads
- Starting Python (Python)
- python extending with c(doubts) (Python)
- Reset and Calculation Buttons (Java)
- Counted loop - Help please (Python)
- Homework Help (Java)
- Homework Help (Community Introductions)
Other Threads in the Python Forum
- Previous Thread: Launch IE from python?
- Next Thread: tuples and lists
| Thread Tools | Search this Thread |
alarm ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp heads homework http ideas import input itunes java leftmouse line linux list lists loop maze module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh statistics string strings sudokusolver sum terminal text thread threading time tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib





