-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyBank.py
More file actions
95 lines (69 loc) · 3.28 KB
/
Copy pathPyBank.py
File metadata and controls
95 lines (69 loc) · 3.28 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
# Import dependencies
import os
import csv
# Define PyBank's variables
months = []
profit_loss_changes = []
count_months = 0
net_profit_loss = 0
previous_month_profit_loss = 0
current_month_profit_loss = 0
profit_loss_change = 0
budget_data = "budget_data.csv"
# Open and read csv
with open("budget_data.csv", newline="") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
# Read the header row first
csv_header = next(csvfile)
#print(f"Header: {csv_header}")
# This prints -->> Header: Date, Profit/Losses
# Read through each row of data after the header
for row in csv_reader:
# Count of months
count_months += 1
# Net total amount of "Profit/Losses" over the entire period
current_month_profit_loss = int(row[1])
net_profit_loss += current_month_profit_loss
if (count_months == 1):
# Make the value of previous month to be equal to current month
previous_month_profit_loss = current_month_profit_loss
continue
else:
# Compute change in profit loss
profit_loss_change = current_month_profit_loss - previous_month_profit_loss
# Append each month to the months[]
months.append(row[0])
# Append each profit_loss_change to the profit_loss_changes[]
profit_loss_changes.append(profit_loss_change)
# Make the current_month_loss to be previous_month_profit_loss for the next loop
previous_month_profit_loss = current_month_profit_loss
#sum and average of the changes in "Profit/Losses" over the entire period
sum_profit_loss = sum(profit_loss_changes)
average_profit_loss = round(sum_profit_loss/(count_months - 1), 2)
# highest and lowest changes in "Profit/Losses" over the entire period
highest_change = max(profit_loss_changes)
lowest_change = min(profit_loss_changes)
# Locate the index value of highest and lowest changes in "Profit/Losses" over the entire period
highest_month_index = profit_loss_changes.index(highest_change)
lowest_month_index = profit_loss_changes.index(lowest_change)
# Assign best and worst month
best_month = months[highest_month_index]
worst_month = months[lowest_month_index]
# -->> Print the analysis to the terminal
print("Financial Analysis")
print("----------------------------")
print(f"Total Months: {count_months}")
print(f"Total: ${net_profit_loss}")
print(f"Average Change: ${average_profit_loss}")
print(f"Greatest Increase in Profits: {best_month} (${highest_change})")
print(f"Greatest Decrease in Losses: {worst_month} (${lowest_change})")
# -->> Export a text file with the results
budget_file = os.path.join("PYBank Text.rtf")
with open(budget_file, "w") as outfile:
outfile.write("Financial Analysis\n")
outfile.write("----------------------------\n")
outfile.write(f"Total Months: {count_months}\n")
outfile.write(f"Total: ${net_profit_loss}\n")
outfile.write(f"Average Change: ${average_profit_loss}\n")
outfile.write(f"Greatest Increase in Profits: {best_month} (${highest_change})\n")
outfile.write(f"Greatest Decrease in Losses: {worst_month} (${lowest_change})\n")