Counted loop - Help please

Thread Solved

Join Date: May 2006
Posts: 3
Reputation: madloch is an unknown quantity at this point 
Solved Threads: 0
madloch madloch is offline Offline
Newbie Poster

Counted loop - Help please

 
0
  #1
May 21st, 2006
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()
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Counted loop - Help please

 
0
  #2
May 21st, 2006
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/techtalkforum...ment114-3.html

  1. print "This program calculates how long it takes for an investment to double in size."
  2. print
  3.  
  4. investment = 1000 #initial investment
  5. rate = input("Please enter the applicable interest rate: ") #interest rate input
  6. years = 0 #sets years to zero
  7. value = investment
  8.  
  9. while value <= investment*2: #defines loop sentinel
  10. value = value + value*((float(rate)/100)) #calculates investment
  11. years = years + 1 #adds a year for every loop
  12.  
  13. print "The investment doubles in",years,"years" #prints the years it took to double the investment
  14.  
  15. 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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3
Reputation: madloch is an unknown quantity at this point 
Solved Threads: 0
madloch madloch is offline Offline
Newbie Poster

Re: Counted loop - Help please

 
0
  #3
May 21st, 2006
Hiya

Thanks for your reply!

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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,019
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Counted loop - Help please

 
0
  #4
May 21st, 2006
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:
  1. print "This program calculates how long it takes for an investment to double in size."
  2. print
  3.  
  4. investment = 1000
  5. rate = input("Please enter the annual interest rate: ")
  6. years = 0
  7. value = investment
  8.  
  9. while value <= investment * 2 :
  10. # the next two indented lines belong to the while loop
  11. value = value + value * float(rate)/100
  12. years = years + 1
  13.  
  14. # this line is not part of the loop! (not indented)
  15. print "At", rate, "% interest your investment doubles in", years, "years"
  16.  
  17. 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 ...
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3
Reputation: madloch is an unknown quantity at this point 
Solved Threads: 0
madloch madloch is offline Offline
Newbie Poster

Re: Counted loop - Help please

 
0
  #5
May 21st, 2006
It worked - thank you!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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