#! /usr/bin/python3 # =================================================================== # Compound Interest # =================================================================== import sys import user_interface as ui # ------------------------------------------------------------------- # ---- test for a positive integer # ------------------------------------------------------------------- def is_positive_integer(s): x,i = ui.is_integer(s) if not x: print() print('Not an integer - try again') return (False,0) if i < 0: print() print('must be a positive integer - try again') return (False,0) return (True,i) # ------------------------------------------------------------------- # ---- display the program description # ------------------------------------------------------------------- def program_description(): ui.clear_screen() print('-------------------- compound Interest --------------------') print('This program calculates compound interest. The user enters:') print(' initial investment (dollars)') print(' annual interest rate (percent)') print(' length of investment (years)') print(' number of times the interest is compounded per year') print('Note: All entered values must be an integers.') # ------------------------------------------------------------------- # ----- get positive integer value # ------------------------------------------------------------------- def get_integer_value(prompt): while(True): print() s = ui.get_user_input(f'{prompt}: ') # ---- empty string? then exit program if not s: print() sys.exit() # ---- verify input x,i = is_positive_integer(s) if not x: continue return (True,i) # ------------------------------------------------------------------- # ---- main # ------------------------------------------------------------------- program_description() while(True): print() print('Enter initial investment amount (dollars)') x,ii = get_integer_value('Initial investment') print() print('annual interest rate (percent)') x,ir = get_integer_value('Interest rate') iir = ir/100 print() print('investment length (years)') x,il = get_integer_value('Investment length') print() print('number of times the interest is compounded per year') x,yc = get_integer_value('Compounded annually') print() print(f'Initial investment (dollars) {ii}') print(f'Interest rate (percent) {ir}') print(f'Length of investment (years) {il}') print(f'Compounded annually (compounded) {yc}') iv = int(ii * pow((1 + iir/yc),(il * yc))) print(f'Investment value {iv}') ui.pause() ui.clear_screen()