r/pythonhelp • • Oct 25 '20

Simple Variable Defining Question

How can I input variable CF properly into total_cashflows to yield

[-40000, 5000, 8000, 12000, 30000].

Code:

import numpy as np

rate = .08

Initial_investment = -40000

CF = [5000, 8000, 12000, 30000]

total_cashflows = [int(Initial_investment), int(CF)]

np.npv(rate, total_cashflows).round(5)

3 Upvotes

2 comments sorted by

3

u/SnooRobots3248 Oct 26 '20

Since you want it at the beginning of the list that you have defined you will need to put the following code

CF.insert(0, Initial_investment)
print(CF) should give you [-40000, 5000, 8000, 12000, 30000]

however your last line of code isn't correct because you can't do this operation on a list. Even if you convert it into a int. consider revising

1

u/[deleted] Oct 26 '20

This exactly. Regarding the last line of code, np.npv takes a number and an anything array-like, returning a float that can be rounded. Although np.npv is deprecated, I suggest using the corresponding function in the numpy-financial library.