# CSCI 150, Spring 2013 # Solution to Interest Calculator # Author: Michael Goldwasser from random import sample, randint #--------------------------------------------------------------------------------- def greeting(): print """This interest calculator will ask you to select an interest rate, followed by a principal value. It will then calculate and display the principal, interest rate, and balance after one year. You will then be invited to execute the process again or terminate. """ #--------------------------------------------------------------------------------- def indexToChar(k): """Utility that converts an index, such as 3, into the appropriate uppercase letter, such as 'D'.""" return chr(ord('A') + k) #--------------------------------------------------------------------------------- def getRate(choices): n = len(choices) for k in range(n): print indexToChar(k) + ') ' + str(choices[k]) + '%' valid = False while not valid: ans = raw_input('Enter A-' + indexToChar(n-1) + ': ').upper() if len(ans) == 1 and 'A' <= ans <= indexToChar(n-1): valid = True else: print 'That is not a valid selection.' return choices[ord(ans) - ord('A')]/100.0 #--------------------------------------------------------------------------------- def getPrincipal(limit): valid = False while not valid: print response = raw_input('Enter the principal: ') if response.startswith('$'): response = response[1:] # remove one leading $ symbol if 0 <= response.find('.') < len(response) - 3: # there are too many characters after a decimal point print 'The principal must be specified in dollars and cents' else: try: principal = float(response) if principal < 0: print 'You must enter a positive amount.' elif principal >= limit: print 'The principal must be less than $' + str(limit) + '.' else: valid = True # Hooray!!! except ValueError: print 'Please enter a number.' return principal #--------------------------------------------------------------------------------- def computeBalance(principal, rate): return principal * (1+rate) #--------------------------------------------------------------------------------- def displayResult(principal, rate, balance): print print 'Initial Principal Interest Rate End of Year Balance' print '=========================================================' print '$%-19.2f %-16.2f $%.2f' % (principal, rate, balance) print #--------------------------------------------------------------------------------- def yesNo(prompt): valid = False while not valid: ans = raw_input(prompt).lower() if ans.startswith('y') or ans.startswith('n'): valid = True return ans.startswith('y') #--------------------------------------------------------------------------------- def main(): greeting() again = True while again: choices = sorted(sample(range(1,20), randint(2,6))) rate = getRate(choices) prin = getPrincipal(1000000) balance = computeBalance(prin, rate) displayResult(prin, rate, balance) again = yesNo('Another Computation [y/n]? ') print print "Quitting program. Bye." #--------------------------------------------------------------------------------- # let's get things started... main()