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


Messages In This Thread
Weird problem with reading from file and performing calculations - by pineapple999 - Jul-25-2019, 12:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel File reading vanjoe198 1 2,054 Mar-31-2021, 11:53 AM
Last Post: snippsat
  reading from a file looseCannon101 14 5,036 Jul-18-2020, 11:29 AM
Last Post: GOTO10
  Handling IO Error / Reading from file Expel 10 4,915 Jul-18-2019, 01:21 PM
Last Post: snippsat
  Reading an Unconventional CSV file OzSbk 2 3,902 May-17-2019, 12:15 PM
Last Post: MvGulik
  Convert number to a different base and make calculations frequency 12 5,718 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,473 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Prime numbers calculations frequency 3 3,016 Nov-27-2018, 07:07 PM
Last Post: micseydel
  Calendar calculations frequency 10 5,687 Nov-13-2018, 07:34 PM
Last Post: frequency
  Reading of structured .mat (matlab) file sumit 2 3,447 May-24-2018, 12:12 PM
Last Post: sumit
  File Reading toxicxarrow 9 5,212 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