-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofit-loss.py
62 lines (48 loc) · 2.27 KB
/
profit-loss.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import csv
def average(numbers):
length = len(numbers)
total = 0.0
for number in numbers:
total += number
return total / length
# FINANCIAL ANALYSIS (results printed to terminal)
input_path = os.path.join('.',"budget_data.csv")
with open(input_path, newline="") as csvfile:
csvreader=csv.reader(csvfile, delimiter=",")
csv_header=next(csvreader)
#print(csv_header)
print(".....................................................")
print("Financial Analysis")
print(".....................................................")
list_of_months = list(csvreader)
print("Total Months: " + str(len(list_of_months)))
profits= [int(i[1]) for i in list_of_months]
months = [i[0] for i in list_of_months]
print("Net Profit/Loss: $" + str(sum(profits)))
profitdiffs = [profits[i]-profits[i-1] for i in range(1,86)]
ave_profit = average(profitdiffs).__round__(2)
max_profit = max(profitdiffs)
min_profit = min(profitdiffs)
for i in range(len(list_of_months)):
if (profits[i]-profits[i-1]) == max_profit:
maxmonth = months[i]
#print(maxmonth)
elif (profits[i]-profits[i-1]) == min_profit:
minmonth = months[i]
#print(minmonth)
print("Average Profit/Loss per month: $" + str(ave_profit))
print("Greatest Increase in Profits: " + str(maxmonth) + " ($"+str(max_profit) +")")
print("Greatest Decrease in Profits: " + str(minmonth) + " ($"+str(min_profit)+")")
# WRITE RESULTS TO TXT
output_file = os.path.join('.', "financial_analysis.txt")
with open(output_file, "w", newline="") as output_file:
output_file.write('.....................................................')
output_file.write('\n Financial Analysis')
output_file.write('\n.....................................................')
output_file.write('\nTotal Months: ' + str(len(list_of_months)))
output_file.write('\nNet Profit/Loss: $' + str(sum(profits)))
output_file.write('\nAverage Profit/Loss per month: $' + str(ave_profit))
output_file.write('\nGreatest Increase in Profits: ' + str(maxmonth) + ' ($' + str(max_profit) + ')')
output_file.write('\nGreatest Decrease in Profits: ' + str(minmonth)+ ' ($' + str(min_profit) + ')')
output_file.close()