Jul-25-2019, 12:38 AM
(This post was last modified: Jul-25-2019, 12:38 AM by pineapple999.)
Here is the instructions:
The trustees of a small college are considering voting a pay raise for their faculty members. They want to grant a 7 percent raise for those earning more than $50,000.00, a 4 percent raise for those earning more than $60,000.00 and 5.5 percent raise for all others. However before doing so, they want to know how much this will cost. Write a program that will print the pay raise for each faculty member, the total amount of the raises, and the average of the raises. Use the end of file as a sentinel value. The input data is available in program7.txt
My problem is, the program will correctly display the original salaries (the contents of the file it reads from), but will only print the raised salary on a few of them. Here is the program I have:
The trustees of a small college are considering voting a pay raise for their faculty members. They want to grant a 7 percent raise for those earning more than $50,000.00, a 4 percent raise for those earning more than $60,000.00 and 5.5 percent raise for all others. However before doing so, they want to know how much this will cost. Write a program that will print the pay raise for each faculty member, the total amount of the raises, and the average of the raises. Use the end of file as a sentinel value. The input data is available in program7.txt
My problem is, the program will correctly display the original salaries (the contents of the file it reads from), but will only print the raised salary on a few of them. Here is the program I have:
def main(): # Declare totalRaise and facultyCount, assign as 0 totalRaise = 0 facultyCount = 0 # Print formatting for old salary and raised salary, # Convert numbers back to strings print("Pay raise for each faculty member: ") print("\n%-15s %-15s\n"%("Old Salary", "Raised Salary")) # This part is required as per the homework template inFile = open('program7.txt', 'r') lineRead = inFile.readline() while lineRead != '': words = lineRead.split() for word in words: num = float(word) print(format(num, '.2f')) lineRead = inFile.readline() # End homework template # calculate raise for salary greater than 60000 if num > 60000.00: raiseSalary = num * (4/100.0) # calculate raise for salary greater than 50000 elif num > 50000.00: raiseSalary = num * (7/100.0) # calculate raise for salary less than 50000 else: raiseSalary = num * (5.5/100.0) # calculate total amount of raises totalRaise += raiseSalary # Display salary raise for each faculty member print(" %.2f "%(num+raiseSalary)) # Increment faculty count facultyCount = facultyCount + 1 # Print total amount of raises print("\n Total amount of raises: " + str(totalRaise)) # Print average of the raises print(" Average of the raises: " + str(totalRaise/float(facultyCount))) inFile.close() main()And here is the output:
Pay raise for each faculty member: Old Salary Raised Salary 52500.00 64029.50 56000.00 50001.00 53501.07 65500.00 42800.00 45000.50 68900.00 71656.00 60000.00 59999.94 54120.25 64100.00 66664.00 44000.50 80100.20 90000.00 41000.00 43255.00 60500.50 72000.00 50000.01 50000.00 52750.00 80001.75 60001.00 62401.04 Total amount of raises: 16225.11 Average of the raises: 2704.185As you can see I'm also having some formatting issues because the old and raised salaries aren't lining up, but my biggest concern is why it's not printing all the raised salaries. Thanks.