Sales Data Analysis
A company has multiple salespeople. Every month, they go on road trips to sell the company's product. At the end of each month, the total sales for each sales person, together with that salesperson's ID and the month, is recorded in a file. At the end of each year, the manager of the company wants to see an Annual Sales Report in the format illustrated in the sample execution below. Your report should look substantially similar to the example execution and the numbers should all be the same.
In this report, QT1 stands for quarter 1 (months 1-3), QT2 for quarter 2 (months 4-6), and so on.
The salespeople's IDs are stored in one file and the sales data is stored in another file. The sales data is in the following form:
SalespersonID month salesAmount
.
.
.
The names of the salesID file and sales data file will be entered by the user when the program is executed. The salesID file and the sales data file are in no particular order. View the two provided files in notepad to see what the files look like after you download them. They should be placed in the same directory where your Final Project .py file will be saved.
Notice that the data in the report is in sorted order by salesID.
The program will have the following functions:
def get_IDs(filename):
Where filename is a parameter which will contain the name of the
file containing the salesperson's IDs.
The get_IDs function will read the salesperson's IDs file and
create two Python lists: a list of the IDs and a list that is a
two-dimensional list of the sales data by quarter for each
salesperson. The four columns will be float values that will
contain the 4 quarters totals in columns 0-3. Two-dimensional lists
will be explained in more detail in the accompanying video. This
list will contain the value 0.0 for all of the entries.The getIDs
functions will return both of these lists.
def process_sales_data(filename, id_list, sales_data):
where filename will contain the name of the file containing the
sales data, and id_list and sales_data are the two Python lists
created by the getIDs function.
The process_sales_data function will read the sales data file and
add all the sales data to the sales_data list totaling all the
monthly data into the totals for the proper quarter by sales ID.
This function will not return any variables but the sales_data list
will have been modified with the sales data.
def print_report(id_list, sales_data):
where id_list and sales_data are the two Python lists created by the getIDs function and modified by the process_sales_data function.
This function will produce the printed Annual Sales Report from the data supplied in the id_list and sales_data lists. It will also calculate the totals by quarter and determine the maximum sales by a sales person in a quarter and maximum sales by quarter for the Annual Sales Report.
def main():
The main function will control the overall flow of the program. It will ask the user for the name of each of the input files and then call the other three functions in order. The program will be executed by a statement calling the main function. Use as many Python built-in functions to do your calculations as possible. It will save you programming.
Sample execution:
salesIDs.txt
12345
32214
23422
57373
68483
99376
salesdata.txt
12345 1 893.33
32214 1 343.50
23422 3 903.22
57373 2 893.55
68483 5 329.00
99376 9 329.66
12345 2 999.99
32214 4 892.77
23422 4 895.33
23422 2 492.77
57373 6 985.84
68483 10 1224.90
99376 11 234.56
12345 8 494.44
68483 7 595.55
12345 12 322.22
68483 9 567.85
99376 3 788.77
99376 12 956.66
32214 8 777.77
32214 10 888.88
57373 9 256.56
PYTHON CODE:
def get_IDs(filename): f=open(filename,"r") line=f.readline() ID=[] while line: ID.append(int(line)) line=f.readline() f.close() return ID def process_sales_data(filename,id_list,sales_data): f=open(filename,"r") line=f.readline() while line: d=line.split(" ") id=int(d[0]) month=int(d[1]) sales=float(d[2]) ind=0 for i in id_list: if i==id: ind=id_list.index(i) quarter=0 if month>=1 and month<=3: quarter=1 elif month>=4 and month<=6: quarter=2 elif month>=7 and month<=9: quarter=3 else: quarter=4 sales_data[ind][quarter-1]+=sales sales_data[ind][quarter-1]=round(sales_data[ind][quarter-1],2) line=f.readline() f.close() if __name__=="__main__": file1=str(input("Enter the name of the sales id file: ")) file2=str(input("Enter the name of the sales data file: ")) ID=get_IDs(file1) size=len(ID) quarter=4 sales_data=[[0 for x in range(quarter)] for y in range(size)] process_sales_data(file2,ID,sales_data) total_id=[] total_quarter=[] for i in range(0,len(ID)): sum=0 for j in range(0,4): sum+=sales_data[i][j] total_id.append(round(sum,2)) for i in range(0,4): sum=0 for j in range(0,len(ID)): sum+=sales_data[j][i] total_quarter.append(round(sum,2)) sum=0 for i in range(0,4): sum+=total_quarter[i] sum=round(sum,2) print("") print("") print("--------Annual Sales Report--------") print("") print("ID QT1 QT2 QT3 QT4 Total") for i in range(0,len(ID)): print(ID[i],end=" ") print(sales_data[i][0],end=" ") print(sales_data[i][1],end=" ") print(sales_data[i][2],end=" ") print(sales_data[i][3],end=" ") print(total_id[i]) print("Total",end=" ") print(total_quarter[0],end=" ") print(total_quarter[1],end=" ") print(total_quarter[2],end=" ") print(total_quarter[3],end=" ") print(sum) print("") max_id=0 ind1=0 for i in range(0,len(ID)): for j in range(0,4): if max_id<sales_data[i][j]: max_id=sales_data[i][j] ind1=i print("Max Sales by Salesperson: ID = ",str(ID[ind1])," Amount = $",str(max_id)) max_quarter=0 ind2=0 for i in range(0,4): if max_quarter<total_quarter[i]: ind2=i max_quarter=total_quarter[i] print("Max Sales by Quarter: Quarter = ",ind2+1," Amount=$",str(max_quarter)) Output:
Sales Data Analysis A company has multiple salespeople. Every month, they go on road trips to sel...
(Python) Write a program called sales.py that uses a list to hold the sums of the sales for each month. The list will have 12 items with index 0 corresponds with month of “Jan”, index 1 with month “Feb”, etc. o Write a function called Convert() which is passed a string which is a month name (ex. “Jan”) and returns the matching index (ex. 0). Hint – use a list of strings to hold the months in the correct order,...
Lab Exercise #15 Assignment Overview This lab exercise provides practice with Pandas data analysis library. Data Files We provide three comma-separated-value file, scores.csv , college_scorecard.csv, and mpg.csv. The first file is list of a few students and their exam grades. The second file includes data from 1996 through 2016 for all undergraduate degree-granting institutions of higher education. The data about the institution will help the students to make decision about the institution for their higher education such as student completion,...
YOU NEED TO MODIFY THE PROJECT INCLUDED AT THE END AND USE THE INCLUDED DRIVER TO TEST YOUR CODE TO MAKE SURE IT WORK AS EXPECTED. Thanks USING PYTHON, complete the template below such that you will provide code to solve the following problem: Please write and/or extend the project5 attached at the end. You will have to do the following for this assignment: Create and complete the methods of the PriceException class. Create the processFile function. Modify the main...