-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainPB.py
More file actions
98 lines (74 loc) · 3.22 KB
/
mainPB.py
File metadata and controls
98 lines (74 loc) · 3.22 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# PyBank
import os
import csv
# set a CSV file path for the data
bank_csv = os.path.join('budget_data.csv')
# define function
def analyze_bank(data):
# define variables
monthsCount = 0
netProfit = 0
monthList = []
monthProfit = 0
change = 0
changeList = []
# start loop through rows
for row in data:
# add 1 to monthsCount for each row
monthsCount += 1
# increase netprofit with each row
netProfit += int(row[1])
# add each month to monthList
monthList.append(str(row[0]))
# Calculate month to month changes in profit
# if there is previous data do the following:
if change != 0:
# set monthProfit to value in profit column
monthProfit = int(row[1])
# subtract current profit from previous month's profits to find the change
change = monthProfit - change
# take this change value and store it in the changeList
changeList.append(change)
# reset change variable to the value in the current profit column, next
change = int(row[1])
# if there is no previous data reset change to value in profit column (this will only apply once for the first row)
elif change == 0:
change = int(row[1])
# remove 1st month from monthList since there is no change that occurs
monthList.pop(0)
# find the index position of the greatest increase in profits
indxmax = changeList.index(max(changeList))
# find the index position of the greatest decrease in profits
indxmin = changeList.index(min(changeList))
# use index positions to find the month that corresponds with max and min values from the changeList
maxChange = (monthList[int(indxmax)], max(changeList))
minChange = (monthList[int(indxmin)], min(changeList))
# take average of the changeList
average = sum(changeList)/float(len(changeList))
average = round(average,2)
# print the results
print(f'Financial Analysis')
print(f'-------------------------------------------')
print(f'Total Months: {monthsCount}')
print(f'Net Profit: {netProfit}')
print(f'Average Monthly Change: {average}')
print(f'Greatest Increase in Profits: {maxChange}')
print(f'Greatest Loss In Profits: {minChange}')
# set the file to write to
bank_output = os.path.join("PyBankResults.txt")
# write the results to a text file
with open(bank_output, 'w') as txtfile:
txtfile.write('Financial Analysis')
txtfile.write('\n------------------------------------')
txtfile.write(f'\nTotal Months: {monthsCount}')
txtfile.write(f'\nNet Profit: {netProfit}')
txtfile.write(f'\nAverage Monthly Change: {average}')
txtfile.write(f'\nGreatest Increase In Profits: {maxChange}')
txtfile.write(f'\nGreatest Loss In Profits: {minChange}')
# read in the CSV file
with open(bank_csv, 'r', newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
# adjust for header
csv_header = next(csvfile)
# use function
analyze_bank(csvreader)