Functions (g06)

# npv.py

"""
npv.py
Spring 2022 PJW
"""

#
#  Function for reading a set of cash flows from a file. Accepts
#  the name of a file and returns a list of payment objects with
#  two attributes: t and amt.
#

pmts = []
fh = open(filename)
for line in fh:
words = line.split()
new_pmt = {
't':int(words[0]),
'amt':float(words[1])
}
pmts.append(new_pmt)

fh.close()

return pmts

#
#  Function for computing the NPV of a list of cash flows. Accepts an
#  interest rate and a list of payment objects.
#

def npv(r,cashflow):

val = 0
for pmt in cashflow:
val = val + pmt['amt']/(1+r)**pmt['t']

return val

#
#  Now call the functions to compute the cash flows of three projects.
#  Do it as a loop here but it could also be done as a sequence of
#  individual calls to read_cashflow() and npv().
#

projects = ['camry','volt','solar']
for proj in projects: