[Tutor] Is this correct?

bob gailer bgailer at gmail.com
Sun Feb 8 23:11:40 CET 2009


For what its worth - here is my revision of your program. I did not send 
it earlier because I kept finding things that led me to want to see and 
verify the fundamental algorithm. But now I think my observations could 
also be useful.

My changes:
  Changed indentation from 8 to 2. For me this is much more readable
   and helps when there are several levels of indentation.
   Others prefer 4
  Removed all blank lines
  Removed unnecessary if statements and started with count == 2
  Changed sign of tuition (makes it a LOT easier to follow arithmetic)
  Calcuate balance then use in print statement avoiding duplicate 
calculation
  Removed unnecessary ()
  Used *= where appropriate
  Used multiple assignment where appropriate
  Changed tuitionInc to 1.041 thus simplifying calculation of new tuition
  interest is misleading - it is the interest!
 
Questions:
  Why calculate tuition increase twice?
  Next has no explanatory text - what is it for?
    print newBalance + interest
  I wonder about if newBalance < tuition:
    With the starting values you provided that case is never true
    If it were true,   principal = newBalance - tuition
      should be under an else clause! 

principal = balance = 500000 # Starting fund amount.
interestRate = .05 # Interest rate added to the principal annually.
tuition = 34986 # Starting tuition fee
tuitionInc = 1.041 # The annual increase applied to the tuition.
print principal, "Is the starting principal" # This is to show the 
starting balance.
print tuition, "Is the starting tuition."
balance = principal - tuition
print balance," the amount remaining after first tuition deduction.  "
interest = balance * interestRate # calculates the amount added to the fund
print interest, "  Interest added to fund for one year. "
newBalance = balance + interest
print newBalance, "is the new balance."
count = 2 # keeps me informed on the number of students benefitting from 
this grant.
while principal > tuition:
  tuition *= tuitionInc # calculates the rising cost of tuition.
  if newBalance < tuition:
    print newBalance + interest
    newBalance = principal
    print principal," is the new balance. "
    interest = principal * interestRate
    print interest," is the new interest on the principal. "
    balance = principal + interest
    print balance,"is the new principal. "
  tuition *= tuitionInc # calculates the rising cost of tuition.
  print tuition," is the new cost of tuition. "
  principal = newBalance - tuition
  print principal," is the new balance. "
  interest = principal * interestRate
  print interest," is the new interest on the principal. "
  balance = principal + interest
  print balance, "is the new principal. "
  count = count + 1
  if tuition > principal:
    print principal + interest, "was available for the last student."
if count > 10:
  print count, "Students used this grant."


-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list