Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

home / study / engineering / computer science / questions and answers / look at

ID: 3792402 • Letter: H

Question

home / study / engineering / computer science / questions and answers / look at the file widget_sales.txt (under resources). ...

Your question has been posted.

We'll notify you when a Chegg Expert has answered. Post another question.

Question: Look at the file widget_sales.txt (under Resources...

Bookmark

EDIT QUESTION

Look at the file widget_sales.txt (under Resources). This is the number of sales over 12 months for a small company that has 6 employees. You will note that each month is separated from the next month by a tab character (represented by   ) .  For example, employee #1 sold 2386 widgets in January, 2176 widgets in Febuary, and 2315 widgets in December.

In Python, write a function called total_sales_emp() that accepts one argument that represents an employee number.  This function should return the total sales of that employee over the year. For example, total_sales_emp(1) would return the total number of widget sales by employee #1 which is 26,563 widgets. Prompt the user for an employee number (tell them it should be between 1 and 6), and then output the number of sales for that employee. Here are some sample runs. Note: The total values shown here are correct – you can use them to make sure your program is doing the calculations properly.

Your output statement strings should match the ones demonstrated below:

3.Using the same file as above, write a function called that simply returns the total widget sales of all employees over the year. (When you test your code, the result should be 157105 widgets). You should use a nested loop to do this calculation.

Employee number (1-6) 1 Employee 1 sold 26563 widgets last year. RESTART: C: /Dropbox /241 Employee number (1-6) 6 Employee 6 sold 26627 widgets last year

Explanation / Answer

employee_sales = {}; def total_sales_emp(emp_number): """ This function take employee number and return total sales :param emp_number: :return: """ print("Employee #", emp_number, "sold", employee_sales[emp_number], "widgets last year") def read_file(file_name): """ This function parse the file :param file_name: :return: """ count = 1 with open(file_name, 'r') as f: for line in f: data = line.strip().split(" ") emp_monthly_sales = 0 for month in data: month_data = month.split(" ") emp_monthly_sales += int(month_data[1]) employee_sales[count] = emp_monthly_sales count += 1 if __name__ == '__main__': read_file("widget_sales.txt") user_input = int(input("Enter Employee Number 1-6:").strip()) total_sales_emp(user_input)