Solutions to Sequential Processing Problems ################################################################## # Problem 1. What is the average salary of all employees? Ans: # ################################################################## # Open input file fin = open("employees.txt", "r") # Initialize count and sum. count = 0 sum = 0.0 # Read and throw away header line. fin.readline( ) # Process file. line = fin.readline( ) while line: fields = line.split(",") salary = float(fields[4].strip( )) sum += salary count += 1 line = fin.readline( ) fin.close( ) if count > 0: print("The average salary of all employees is $", end="") print(round(sum / count, 2)) else: print("There are no employees in input file.") ################################################################## # Problem 2. The average salary for all women. # ################################################################## # Open input file fin = open("employees.txt", "r") # Initialize count and sum. count = 0 sum = 0.0 # Read and throw away header line. fin.readline( ) # Process file. line = fin.readline( ) while line: fields = line.split(",") gender = fields[1].strip( ) salary = float(fields[4].strip( )) if gender == "F": sum += salary count += 1 line = fin.readline( ) fin.close( ) if count > 0: print("The average salary of", end=" ") print("all women employees is $", end="") print(round(sum / count, 2)) else: print("There are no women employees in input file.") ################################################################## # 3. The average age of the male employees in Chicago. # ################################################################## # Open input file fin = open("employees.txt", "r") # Initialize count and sum. count = 0 sum = 0.0 # Read and throw away header line. fin.readline( ) # Process file. line = fin.readline( ) while line: fields = line.split(",") gender = fields[1].strip( ) city = fields[3].strip( ) salary = float(fields[4].strip( )) if gender == "M" and city == "Chicago": sum += salary count += 1 line = fin.readline( ) fin.close( ) if count > 0: print("The average salary of", end=" ") print("all male Chicago employees is $", end="") print(round(sum / count, 2)) else: print("There are no male Chicago", end=" ") print("employees in input file.") 4. Who is the oldest employee? What is his or her age? This problem will be discussed on Apr 18. 5. The name and age of the oldest women employee. This problem will be discussed on Apr 18. ##################################################################### # Problem 4. The average salary of employees in the desired city, # where the desired city is entered from the keyboard. # ##################################################################### # Enter desired city at keyboard. desired_city = input("Enter desired city: ") # Open input file fin = open("employees.txt", "r") # Initialize count and sum. count = 0 sum = 0.0 # Read and throw away header line. fin.readline( ) # Process file. line = fin.readline( ) while line: fields = line.split(",") city = fields[3].strip( ) salary = float(fields[4].strip( )) if city == desired_city: sum += salary count += 1 line = fin.readline( ) fin.close( ) if count > 0: print("The average salary of", end=" ") print(f"all male {desired_city}", end=" ") print(f"employees is ${round(sum / count, 2)}.") else: print(f"There are no {desired_city}", end=" ") print("employees in input file.")