| | |
Counted loop - Help please
Thread Solved |
•
•
Join Date: May 2006
Posts: 3
Reputation:
Solved Threads: 0
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()
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 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
Also, some of your statement lines are so obvious that comment is simply not needed, or is distraction at best!
http://www.daniweb.com/techtalkforum...ment114-3.html
Python Syntax (Toggle Plain Text)
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 raw_input("Press enter to go on ...") # optional console wait
•
•
Join Date: May 2006
Posts: 3
Reputation:
Solved Threads: 0
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?
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?
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:
The display shows:
Python Syntax (Toggle Plain Text)
print "This program calculates how long it takes for an investment to double in size." 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
•
•
•
•
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!
![]() |
Similar Threads
- Loop...without the loop (Java)
Other Threads in the Python Forum
- Previous Thread: wxpython gdk error on fedora 5 (fc5)
- Next Thread: ps2pdf problems in Python
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






