I'm working with a CSV file and want to output:
- Total sales
- Total sales for
- Lowest month sales
- Highest monthly sales
Code
import csv
def read_data():
data = []
with open('sales.csv', 'r') as sales_csv:
spreadsheet = csv.DictReader(sales_csv)
for row in spreadsheet:
data.append(row)
return data
def run():
data = read_data()
sales_by_month = {}
total_sales = {}
for row in data:
month = row['month']
sale = int(row['sales'])
if month not in sales_by_month:
sales_by_month[month] = []
sales_by_month[month].append(sale)
for month, sales in sales_by_month.items():
total_sales = sum(sales)
print('Total sales for {}: {}'.format(month, total_sales))
run()
def run():
data = read_data()
sales = []
for row in data:
sale = int(row['sales'])
sales.append(sale)
total = sum(sales)
print('Total sales: {}'.format(total))
run()
def run():
data = read_data()
lowest = []
for row in data:
month = row['month']
sale = int(row['sales'])
lowest.append(sale)
lowest_month = min(lowest)
print('Lowest month sales in {}: {}'.format(month, lowest_month))
run()
Issue
My code works, except the Lowest month sales and haven't done the highest monthly yet.
The value for 'sales amount' comes back correct, but it prints the wrong month. The month comes back as dec
(should be feb
).
Question
How do I get the correct month for lowest monthly sales?