RichardMotion RichardMotion - 6 months ago 14
Python Question

How to calculate the average from a list of records

So i am working on an Assignment and seem to be having trouble when it comes to calculating an average from a list of data (the data is read from an external .txt file). Specifically what i am trying to do is to read data records from the list of data below.

1-2-2014,Frankton,42305.67,23
12-4-2014,Glenview,21922.22,17
10-2-2015,Glenview,63277.9,32
10-5-2015,Glenview,83290.09,16
11-6-2015,Frankton,81301.82,34
10-10-2015,Frankton,62333.3,40
11-11-2015,Frankton,28998.8,29
1-1-2016,Glenview,51083.5,27
1-3-2016,Glenview,62155.72,42
27-3-2016,Frankton,33075.1,18
2-4-2016,Glenview,61824.7,35


After the above data has been read and stored as records e.g.

1-2-2014 as [rec0]Frankton[rec1]42305.67[rec2]67,23[rec3].

I am needing to prompt the user to enter a store name (either Frankton or Glenview) from the list of records, then calculate the total rec2 from all the records combined that are from the user's entered store. I am also needing to do the same for the average, e.g. calculate the average of rec[3] from the user's entered store.

Currently the total and total transaction is printing correct but the average and average transaction is not displaying with the correct numbers.

The correct output i am looking for when i run this program will be -

The user enters Frankton

enter image description here
or the user enters Glenview

enter image description here

My current code will be posted below:

from datetime import datetime

def readdata(filename):
print('*' * 10, 'Reading Records From ', filename, '*' * 10)
print('Done.\n\n')
data = []
readf = open(filename, 'r')
for line in readf :
datestr, branch, dailySale, transactions = line.split(',')
dateobj = datetime.strptime(datestr, '%d-%m-%Y')
rec = [dateobj, branch, eval(dailySale), int(transactions)]
data.append(rec)
readf.close()

return data

def query_branch_sale(records):
print('*' * 10, 'Querying Total Sales by Branch', '*' * 10)
branch = input('Enter branch name (Glenview or Frankton): ')
while branch != 'Glenview' and branch != 'Frankton' :
print('Invalid. Enter again.')
branch = input('Enter branch name (Glenview or Frankton): ')
total = 0
total_transaction = 0
for rec in records:
if rec[1] == branch:
total = total + rec[2]
total_transaction = total_transaction + rec[3]
average = total/len(records)
ave_transaction = total_transaction/len(records)
print(total)
print(average)
print(total_transaction)
print(ave_transaction)

def main():

records = readdata('data.txt')
query_branch_sale(records)

main ()

Answer

You shouldn't divide by len(records) when calculating the averages, because records includes the transactions from other branches. You need to add a counter variable that counts the number of transactions from the selected branch, and divide by that.

total = 0
total_transaction = 0
count = 0
for rec in records: 
    if rec[1] == branch:
        total = total + rec[2]
        total_transaction = total_transaction + rec[3]
        count += 1
if count > 0:
    average = total/count
    ave_transaction = total_transaction/count
else:
    average = 0
    ave_transaction = 0