[Tutor] loop running twice?
Byron Ruffin
byron.ruffin at g.austincc.edu
Sun Nov 17 02:00:47 CET 2013
def main():
goal, apr, deposit = getSavingsDetails()
determineMonthsTilSaved( goal, apr, deposit )
months = determineMonthsTilSaved(goal, apr, deposit)
summarize( months )
def getSavingsDetails():
"""
goal = float( input( "Principal sought? $" ) )
apr = float( input( "Interest rate? " ) )
deposit = float( input( "Deposit? $" ) )
"""
goal = 1000.0
apr = .05
deposit = 100
return goal, apr, deposit
def determineMonthsTilSaved( goal, apr, deposit ):
months = 0
saved = 0
totalInterest = 0.0
while saved < goal:
interest = saved * apr / 12
totalInterest += interest
saved += (deposit + interest)
months += 1
print( months, ("%.2f" % saved), ("%.2f" % totalInterest) )
return months
def summarize( months ):
print( "Saved! It took ", months // 12, "years and", months % 12,
"months." )
main()
When this is run it appears that determineMonthsTilSaved is running twice
before the loop ends. It is supposed to run until saved > than goal, but
look at the output. It runs again even after saved > goal. Help please?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131116/26f2f3b0/attachment-0001.html>
More information about the Tutor
mailing list