Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 13 - Files and Exception Handling - Programming Exercises - Page 474: 13.17

Answer

code

Work Step by Step

# 13.17 (Process large dataset) A university posts its employee salary at http://cs # .armstrong.edu/liang/data/Salary.txt. Each line in the file consists of faculty first # name, last name, rank, and salary (see Exercise 13.16). Write a program to display # the total salary for assistant professors, associate professors, full professors, and # all faculty, respectively, and display the average salary for assistant professors, # associate professors, full professors, and all faculty, respectively. import urllib from urllib import request #file = open("database.txt", "r") file = urllib.request.urlopen("http://cs.armstrong.edu/liang/data/Salary.txt") data = file.readlines() sumAssistant = 0 countAssistant = 0 sumAssociate = 0 countAssociate = 0 sumFull = 0 countFull = 0 for line in data: line = line.split() if line[2] == "assistant": sumAssistant += eval(line[3]) countAssistant += 1 elif line[2] == "associate": sumAssociate += eval(line[3]) countAssociate += 1 else: sumFull += eval(line[3]) countFull += 1 print("The total salary for assistant professors: ", sumAssistant) print("The total salary for associate professors: ", sumAssociate) print("The total salary for full professors: ", sumFull) print("The total salary for all faculty: ", (sumFull + sumAssociate + countAssistant)) print("The average salary for assistant professors: ", sumAssistant / countAssistant) print("The average salary for associate professors: ", sumAssociate / countAssociate) print("The average salary for full professors: ", sumFull / countFull) print("The average salary for all faculty: ", (sumFull + sumAssociate + countAssistant) / (countAssociate + countFull + countAssistant))
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.