Hi All

I'm new to Python and have created the below little program but would like to be able to print the loop final count (eg. 8 years) rather than every loop. Any hints on how to do this?

def main():
print "This program calculates how long it takes for an investment to double in size."

investment = 1000 #initial investment
rate = input("Please enter the applicable interest rate: ") #interest rate input
years = 0 #sets years to zero
value = investment

while value <= investment*2: #defines loop sentinel
value = value +value*((float(rate)/100)) #calculates investment
years = years + 1 #adds a year for every loop
print "The investment doubles in",years,"years" #prints the years it took to double the investment
main()

## All 4 Replies

All statement lines that belong to while loop get an indentation of 4 spaces (4 is traditional with Python code). I have removed main() as that is throwback to C. Also wrap your code in code tags,see:
http://www.daniweb.com/techtalkforums/announcement114-3.html

``````print "This program calculates how long it takes for an investment to double in size."
print

investment = 1000 #initial investment
rate = input("Please enter the applicable interest rate: ") #interest rate input
years = 0 #sets years to zero
value = investment

while value <= investment*2: #defines loop sentinel
value = value + value*((float(rate)/100)) #calculates investment
years = years + 1 #adds a year for every loop

print "The investment doubles in",years,"years" #prints the years it took to double the investment

raw_input("Press enter to go on ...")  # optional console wait``````

Also, some of your statement lines are so obvious that comment is simply not needed, or is distraction at best!

Hiya

Unfortunately this still doesn't help me print the output I want. The program prints:

This program calculates how long it takes for an investment to double in size.
The investment doubles in 1 years
The investment doubles in 2 years
The investment doubles in 3 years
The investment doubles in 4 years
The investment doubles in 5 years
The investment doubles in 6 years
The investment doubles in 7 years
The investment doubles in 8 years

And I just want to be able to print the final line:

The investment doubles in 8 years.

Any ideas?

You got to make sure that the statements that belong to the while loop (after the colon) are blocked by indentation. The print statement is not part of the loop! Look at the code carefully:

``````print "This program calculates how long it takes for an investment to double in size."
print

investment = 1000
rate = input("Please enter the annual interest rate: ")
years = 0
value = investment

while value <= investment * 2 :
# the next two indented lines belong to the while loop
value = value + value * float(rate)/100
years = years + 1

# this line is not part of the loop!  (not indented)
print "At", rate, "% interest your investment doubles in", years, "years"

raw_input("Press enter to go on ...")  # optional console wait``````

The display shows:

This program calculates how long it takes for an investment to double in size.

Please enter the annual interest rate: 10
At 10 % interest your investment doubles in 8 years
Press enter to go on ...

It worked - thank you!!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.