Reputation: 17
So I'm really new to Python and have been asked to write a code to track monthly income, expenses, net results, and net present value based on an inputted interest rate.
I'm trying to organize it based on the 12 months and then putting a line for Income, Expenses, and net value for each month, with the interest rate coming at the end of the year.
So far I've only been able to organize the months from 1 to 12 and can't get the income or expenses lines within each month, let alone connect them to the user inputs. Any advice on the next steps would be appreciated. Here is what the question is asking for:
Your program should first ask the user for a monthly interest rate. Then your program should loop while the user types some of the incomes and expenses of the project. After the user is done typing incomes and expenses, for example by just hitting enter without typing anything, your program should write (1) a summary of incomes, expenses, and net result for each month in which the user gave any information, with months in increasing order; and (2) the net present value of the project at month 0 based on the summarized cash flow.
Let's suppose that the user typed a monthly interest rate of 1% as "0.01", and then the user typed an expense of 200 in month 0 as "0 -200", an income of 1000 in month 2 as "2 1000", another income of 200 in month 2 as "2 200", and an expense of 500 in month 2 as "2 -500". In that case, your program should print something like this:
Month 0:
Incomes: 0
Expenses: 200
Net result: -200
Month 2:
Incomes: 1200
Expenses: 500
Net result: 700
Net present value at month 0: 486.21
for i in range(1,13):
print("Month", i,":")
my_tab = {"Income": 0,
"Expenses": 0}
incomes_list = []
expenses_list = []
interest_rate = input("Please enter the interest rate")
while True:
input_month_and_value = input("Please enter a month from 1-12 and the corresponding income or expense")
if input_month_and_value == "":
break
input_month_and_value_list = input_month_and_value.split()
month = (input_month_and_value_list[0])
value = float(input_month_and_value_list[1])
if month in my_tab:
my_tab[month] += value
else:
my_tab[month] = value
if value > 0:
incomes_list.append(input_month_and_value)
if value < 0:
expenses_list.append(input_month_and_value)
print(my_tab)
Upvotes: 2
Views: 8356
Reputation: 17166
Here's some example code that performs your basic functions
import sys
from collections import defaultdict
import numpy as np
def get_monthlys():
" Places inputs as list of tuples (month, amount) "
inputs = []
while True:
input_val = input("Month (1-12) and income or expense (space separated): ")
if input_val == "":
break
month, amount = input_val.split()
month, amount = int(month), float(amount)
inputs.append((month, amount))
return inputs
def groupby_month(inputs):
" Groups cash flow for each month "
cashflow = defaultdict(list)
for month, amount in inputs:
cashflow[month].append(amount)
return cashflow
def show_monthlys(cashflow):
s = sorted(cashflow.items(), key = lambda x: x[0])
for month, amounts in s:
incomes = [v for v in amounts if v >= 0]
expenses = [-v for v in amounts if v < 0]
net = sum(incomes) - sum(expenses)
print(f'Month: {month}')
print(f'Incomes: {" ".join(map(str, incomes)) if incomes else None}')
print(f'Expenses: {" ".join(map(str, expenses)) if expenses else None}')
print(f'Net for Month: {net}')
print()
def calculate_npv(interest_rate, cashflow):
" NPV "
min_month, max_month = min(cashflow.keys()), max(cashflow.keys())
totals = np.zeros(max_month - min_month + 1, dtype = 'f')
for month, amounts in cashflow.items():
month_index = month - min_month
totals[month_index] = sum(amounts)
return np.npv(interest_rate, totals)
# Get inputs
rate_input = input("Monthly Interest rate: ")
try:
interest_rate = float(rate_input)
except ValueError:
print("Error-->> interest rate should be numeric")
sys.exit()
inputs = get_monthlys()
cashflow = groupby_month(inputs)
# Monthly Reports
show_monthlys(cashflow)
# Net Present Value
print(f'Net Present Value: {calculate_npv(interest_rate, cashflow):.2f}')
Example Run (months can input in any order and can even skip months)
Inputs:
Monthly Interest rate: .01
Month (1-12) and income or expense (space separated): 1 200.5
Month (1-12) and income or expense (space separated): 2 300
Month (1-12) and income or expense (space separated): 3 125.4
Month (1-12) and income or expense (space separated): 1 -95
Month (1-12) and income or expense (space separated): 4 650.25
Month (1-12) and income or expense (space separated): 5 95.77
Month (1-12) and income or expense (space separated): 6 800.25
Month (1-12) and income or expense (space separated): 1 155.65
Month (1-12) and income or expense (space separated): 2 -99.99
Month (1-12) and income or expense (space separated): 5 295.77
Month (1-12) and income or expense (space separated): 8 901
Month (1-12) and income or expense (space separated): 9 1024
Month (1-12) and income or expense (space separated): 8 -200
Month (1-12) and income or expense (space separated):
Outputs:
Month: 1
Incomes: 200.5 155.65
Expenses: 95.0
Net for Month: 261.15
Month: 2
Incomes: 300.0
Expenses: 99.99
Net for Month: 200.01
Month: 3
Incomes: 125.4
Expenses: None
Net for Month: 125.40
Month: 4
Incomes: 650.25
Expenses: None
Net for Month: 650.25
Month: 5
Incomes: 95.77 295.77
Expenses: None
Net for Month: 391.54
Month: 6
Incomes: 800.25
Expenses: None
Net for Month: 800.25
Month: 8
Incomes: 901.0
Expenses: 200.0
Net for Month: 701.00
Month: 9
Incomes: 1024.0
Expenses: None
Net for Month: 1024.00
Net Present Value: 3950.39
Upvotes: 1