|
8 | 8 | change_list = [] |
9 | 9 | total_change = 0 |
10 | 10 | months = [] |
11 | | -total_m = 0 |
12 | | -net_total=0 |
| 11 | +total_m = 1 |
| 12 | +net_total= 0 |
| 13 | +file_to_output = os.path.join("budget_analysis.txt") |
13 | 14 |
|
14 | 15 |
|
15 | 16 | budget_csv = os.path.join('.','Resources','budget_data.csv') |
16 | 17 | with open(budget_csv, 'r') as csvfile: |
17 | 18 | csvreader = csv.reader(csvfile, delimiter=',') |
18 | 19 | header = next(csvreader) |
19 | | - previous_row = next(csvreader) |
| 20 | + first_row = next(csvreader) |
| 21 | + previous_row = int(first_row[1]) |
| 22 | + net_total = int(first_row[1]) |
| 23 | + |
20 | 24 | for row in csvreader: |
21 | 25 |
|
22 | 26 | net_total+=int(row[1]) |
23 | 27 | total_m = total_m+1 |
24 | | - change_value = int(row[1]-previous_row) |
| 28 | + current_value = int(row[1]) |
| 29 | + |
| 30 | + change_value = int(current_value-previous_row) |
25 | 31 |
|
26 | 32 | change_list.append(change_value) |
27 | 33 | months.append(row[0]) |
28 | | - previous_row = row |
| 34 | + previous_row = int(row[1]) |
29 | 35 |
|
30 | 36 | total_change = total_change + change_value |
31 | 37 | if change_value > greatest_increase[1]: |
32 | 38 | greatest_increase[0] = str(row[0]) |
33 | 39 | greatest_increase[1] = change_value |
34 | 40 |
|
| 41 | + |
35 | 42 | if change_value < greatest_decrease[1]: |
36 | 43 | greatest_decrease[0] = str(row[0]) |
37 | 44 | greatest_decrease[1] = change_value |
38 | 45 |
|
39 | 46 |
|
40 | | - print (total_change) |
41 | | - print(net_total) |
42 | | - print (total_m) |
43 | | - avg_change = total_change/len(months) |
44 | | - print(avg_change) |
45 | | - |
| 47 | + avg_change = total_change/len(months) |
| 48 | + |
| 49 | +output = ( |
| 50 | + f"\nFinancial Analysis\n" |
| 51 | + f"----------------------------\n" |
| 52 | + f"Total Months: {total_m}\n" |
| 53 | + f"Total: ${net_total}\n" |
| 54 | + f"Average Change: ${avg_change:.2f}\n" |
| 55 | + f"Greatest Increase in Profits: {greatest_increase[0]} (${greatest_increase[1]})\n" |
| 56 | + f"Greatest Decrease in Profits: {greatest_decrease[0]} (${greatest_decrease[1]})\n") |
| 57 | + |
| 58 | +with open(file_to_output, "w") as txt_file: |
| 59 | + txt_file.write(output) |
| 60 | + |
| 61 | + file_to_output |
46 | 62 |
|
47 | | -print("Financial Analysis") |
48 | | - |
49 | | -print("--------------------------") |
50 | | - |
51 | | -print(f"Total Months:{str(total_m)}") |
52 | | - |
53 | | -print(f"Total:{int(net_total)}") |
54 | | - |
55 | | -print(f"Average_change : {int(avg_change)}") |
56 | | - |
57 | | -print(f"Greatest Increase in profit:{ greatest_increase('['+str(row)+']') + change_value}") |
58 | | - |
59 | | -print(f"Greatest Decrease in profit:{ greatest_decrease('['+str(row)+']') + change_value}") |
60 | 63 |
|
61 | 64 |
|
62 | 65 |
|
|
0 commit comments