Python Forum
Weird problem with reading from file and performing calculations
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weird problem with reading from file and performing calculations
#1
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:

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.185
As 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.
Reply
#2
It would appear there is more than one salary per line in the text file. The for loop on line 17 reads through each salary on a single line. Then your code calculates the raise for the last one and prints it. You will need to move your code for calculating the raise and tracking the total into the for loop. Also, you will need to modify the print statement in the for loop in order to get the salary and the salary with the raise to be on the same line.

BTW, that's a really clunky way to read through a file. I realize your teacher wrote it, but just so you know, here's a better form:

   inFile = open('program7.txt', 'r')

   for line in inFile:
       words = lineRead.split()
       for word in words:
            num = float(word)
            print(format(num, '.2f'))
That's a couple lines simpler, and simpler means less like to cause errors and easier to maintain. Speaking of errors, this is even better:

with open('program7.txt', 'r') as inFile:

    for line in inFile:
        words = lineRead.split()
        for word in words:
             num = float(word)
             print(format(num, '.2f'))
The with clause makes sure the file is properly closed if any errors pop up while trying to read it.

That print call is horrid too. You should look into the format method of strings, or the even newer f-string syntax (Python 3.6+).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel File reading vanjoe198 1 2,026 Mar-31-2021, 11:53 AM
Last Post: snippsat
  reading from a file looseCannon101 14 4,871 Jul-18-2020, 11:29 AM
Last Post: GOTO10
  Handling IO Error / Reading from file Expel 10 4,805 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,865 May-17-2019, 12:15 PM
Last Post: MvGulik
  Convert number to a different base and make calculations frequency 12 5,607 Dec-17-2018, 05:21 PM
Last Post: frequency
  reading text file and writing to an output file precedded by line numbers kannan 7 10,368 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Prime numbers calculations frequency 3 2,976 Nov-27-2018, 07:07 PM
Last Post: micseydel
  Calendar calculations frequency 10 5,589 Nov-13-2018, 07:34 PM
Last Post: frequency
  Reading of structured .mat (matlab) file sumit 2 3,404 May-24-2018, 12:12 PM
Last Post: sumit
  File Reading toxicxarrow 9 5,165 May-07-2018, 04:12 PM
Last Post: toxicxarrow

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020