[Tutor] loop running twice?

Alan Gauld alan.gauld at btinternet.com
Sun Nov 17 09:50:31 CET 2013


On 17/11/13 01:00, Byron Ruffin wrote:
 > 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.

You haven't sent the output, just the code...

 > It runs again even after saved goal.
 > Help please?

Look closely at main()

> def main():
>
>      goal, apr, deposit = getSavingsDetails()
>      determineMonthsTilSaved( goal, apr, deposit )
>
>      months = determineMonthsTilSaved(goal, apr, deposit)

You call the function twice, so it runs twice.

> 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

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list