I have a python question
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. Also, print the total faculty payroll before and after the raise. Use the end of file as a sentinel value. The input data is available in program7.txt on the I: drive. Do NOT use a logical operator in the program So far, all I have is this, and I am stuck on how to finish it. program7.txt: 52500.00 64029.50 56000.00 50001.00 65500.00 42800.00 45000.50 68900.00 60000.00 59999.94 54120.25 64100.00 44000.50 80100.20 90000.00 41000.00 60500.50 72000.00 50000.01 50000.00 80001.75 60001.00 Program: def main(): inFile = open('program7.txt', 'r') lineRead = inFile.readline() while lineRead != '': words = lineRead.split() for word in words: num = float(word) print(format(num, '.2f')) percentRaise() lineRead = inFile.readline() # Close the file. inFile.close() def percentRaise(): while lineRead <= 50000: salary = word * .55 newsal = word + salary print("New Salary = ", newsal) # Call the main function. main()
First of all I would like you to show you a trick to enter code here in OS so that it can be copied without having to put in all end of line and idents back in again: enter three \` at the ~ key (you have to enter a space after each to have it turn up). This ``` makes a new block start. Start it at a new line, then enter your code and then at a new line enter three ``` again ``` def percentRaise(): while lineRead <= 50000: salary = word * .55 newsal = word + salary print("New Salary = ", newsal) # Call the main function. main() ``` That way you can copy the code without loosing end of lines and idents
So, the program may not make use of any logical operators: and, or, not
I think, your function def percentRaise(): lacks a return statement
and you need to send the relevant variables to the functions you use
@immanuelv In def percentRaise() you use use ``` while lineRead <= 50000: ``` You probably mean 'word'
You also use ``` salary = word * .55 ``` but that is 55%, you probably mean 0.055
Itś best to break up the assignment into chunks that you can solve 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 a 5.5 percent raise for all others. in pseudocode that could be more like if salary > 60 000 then raise=0.07 # remember that 7% is 0.07 elif salary > 50 000 then raise=0.04 # this is valid for all salaries > 50 000 and smaller than 60 000 else raise=0.055 ``` if salary > 60000:: raise=0.07 elif salary > 50 000: # 50 000< salary < 60 000 raise=0.04 else: raise=0.055 ```
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 pay raise= salary*raise print pay raise Python 2.7 ``` pay_raise = salary*raise print salary, pay_raise ``` Python 3 something like ? ``` pay_raise = salary*raise print(format(salary, '.2f'), format(pay_raise, '.2f'), ```
the total amount of the raises, and the average of the raises. total amount of the pay raises = sum over all pay raises average pay raise = sum over all pay raises/nr of salaries So after each calculation of the raise, you need to keep track of the totoal ``` total_pay_raise += pay_raise ``` You also need to count the number of salaries: so for instanceafter words = lineRead.split() ``` nr_of_sal += len(words) ``` Now average pay raise = sum over all pay raises/nr of salaries could be coded as ``` averrage_pay_rase = total_pay_raise / nr_of_sal ``` ``` average_pay_raise = total_pay_raise
forget the last line, that is some kind of glitch
Also, print the total faculty payroll before and after the raise. total faculty payroll before the raise = sum over all salaries total faculty payroll after the raise = total faculty payroll + total amount of the raises so we also need to sum over all salaries that are read from the file ``` total_sal_before += salary total_sal_after = total_sal_before + total_pay_raise ```
Now you've got all the ingredients from which you can improve your code
in my code snippets, you cannot use the word 'raise', because it is a keyword in Python
Your overall structure is quite well You do need to pass each salary to the function and you need to return the pay_raise back to your main() function and you need to pass that return value to the pay_raise where you call it ``` pay_raise = percentRaise(salary) # you used 'num', where I use 'salary' ```
thank you so much
btw @abtster i did it almost the way you said it midSalary = 50000 maxSalary = 60000 def main(): inFile = open('program7.txt', 'r') lineRead = inFile.readline() while lineRead != '': words = lineRead.split() total = 0.0 ntotal = 0.0 for word in words: num = float(word) total += num print("\nFaculty Member:$", format(num, '.2f'), sep ="") if num >= maxSalary: prcntRaise = .04 salRaise = num * prcntRaise newSal = num + salRaise print("Your pay raise percent will be:", format(prcntRaise*100, ".1f")+'%') print("Your pay raise will be:$", format(salRaise, ',.2f'), sep ="") print("Your new salary will be:", format(newSal, ',.2f'), sep = "") ntotal += newSal elif num >= midSalary: prcntRaise = .07 salRaise = num * prcntRaise newSal = num + salRaise print("Your pay raise percent will be:", format(prcntRaise*100, ".1f")+'%') print("Your pay raise will be:", format(salRaise, ',.2f'), sep ="") print("Your new salary will be:", format(newSal, ',.2f'), sep ="") ntotal += newSal else: prcntRaise = .055 salRaise = num * prcntRaise newSal = num + salRaise salAvg = num / newSal print("Your pay raise percent will be:", format(prcntRaise*100 ,".1f")+'%') print("Your pay raise will be:", format(salRaise, ',.2f'), sep ="") print("Your new salary will be:", format(newSal, ',.2f'), sep ="") ntotal += newSal lineRead = inFile.readline() print("\nTotal Faculty payroll:$", format(total , ",.2f"),sep ="") print("The New Faculty payroll:$", format(ntotal , ",.2f"),sep ="") # Close the file. inFile.close()
Does it work ok?
I think your initialisations have to be befrore the while loop ``` total = 0.0 ntotal = 0.0 while lineRead != '': words = lineRead.split() ```
my code works but this is my solution
Faculty Member:$52500.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,675.00 Your new salary will be:56,175.00 Faculty Member:$64029.50 Your pay raise percent will be: 4.0% Your pay raise will be:$2,561.18 Your new salary will be:66,590.68 Faculty Member:$56000.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,920.00 Your new salary will be:59,920.00 Faculty Member:$50001.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,500.07 Your new salary will be:53,501.07 Total Faculty payroll:$222,530.50 The New Faculty payroll:$236,186.75 Faculty Member:$65500.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,620.00 Your new salary will be:68,120.00 Faculty Member:$42800.00 Your pay raise percent will be: 5.5% Your pay raise will be:2,354.00 Your new salary will be:45,154.00 Faculty Member:$45000.50 Your pay raise percent will be: 5.5% Your pay raise will be:2,475.03 Your new salary will be:47,475.53 Faculty Member:$68900.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,756.00 Your new salary will be:71,656.00 Total Faculty payroll:$222,200.50 The New Faculty payroll:$232,405.53 Faculty Member:$60000.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,400.00 Your new salary will be:62,400.00 Faculty Member:$59999.94 Your pay raise percent will be: 7.0% Your pay raise will be:4,200.00 Your new salary will be:64,199.94 Faculty Member:$54120.25 Your pay raise percent will be: 7.0% Your pay raise will be:3,788.42 Your new salary will be:57,908.67 Faculty Member:$64100.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,564.00 Your new salary will be:66,664.00 Total Faculty payroll:$238,220.19 The New Faculty payroll:$251,172.60 Faculty Member:$44000.50 Your pay raise percent will be: 5.5% Your pay raise will be:2,420.03 Your new salary will be:46,420.53 Faculty Member:$80100.20 Your pay raise percent will be: 4.0% Your pay raise will be:$3,204.01 Your new salary will be:83,304.21 Faculty Member:$90000.00 Your pay raise percent will be: 4.0% Your pay raise will be:$3,600.00 Your new salary will be:93,600.00 Faculty Member:$41000.00 Your pay raise percent will be: 5.5% Your pay raise will be:2,255.00 Your new salary will be:43,255.00 Total Faculty payroll:$255,100.70 The New Faculty payroll:$266,579.74 Faculty Member:$60500.50 Your pay raise percent will be: 4.0% Your pay raise will be:$2,420.02 Your new salary will be:62,920.52 Faculty Member:$72000.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,880.00 Your new salary will be:74,880.00 Faculty Member:$50000.01 Your pay raise percent will be: 7.0% Your pay raise will be:3,500.00 Your new salary will be:53,500.01 Faculty Member:$50000.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,500.00 Your new salary will be:53,500.00 Total Faculty payroll:$232,500.51 The New Faculty payroll:$244,800.53 Faculty Member:$80001.75 Your pay raise percent will be: 4.0% Your pay raise will be:$3,200.07 Your new salary will be:83,201.82 Faculty Member:$60001.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,400.04 Your new salary will be:62,401.04 Total Faculty payroll:$140,002.75 The New Faculty payroll:$145,602.86
absolutely
after every 4 payroll calculation it gives me total salary
I see
i think the part you said about "initialisations have to be befrore the while loop" fixed that problem
nvm
Do not forget the average raise part Write a program that will print print the total amount of the raises, print the average of the raises.
this is my new code and its solution i have not put average raise yet midSalary = 50000 maxSalary = 60000 def main(): inFile = open('program7.txt', 'r') lineRead = inFile.readline() total = 0.0 ntotal = 0.0 while lineRead != '': words = lineRead.split() for word in words: num = float(word) total += num print("\nFaculty Member:$", format(num, '.2f'), sep ="") if num >= maxSalary: prcntRaise = .04 salRaise = num * prcntRaise newSal = num + salRaise print("Your pay raise percent will be:", format(prcntRaise*100, ".1f")+'%') print("Your pay raise will be:$", format(salRaise, ',.2f'), sep ="") print("Your new salary will be:", format(newSal, ',.2f'), sep = "") ntotal += newSal elif num >= midSalary: prcntRaise = .07 salRaise = num * prcntRaise newSal = num + salRaise print("Your pay raise percent will be:", format(prcntRaise*100, ".1f")+'%') print("Your pay raise will be:", format(salRaise, ',.2f'), sep ="") print("Your new salary will be:", format(newSal, ',.2f'), sep ="") ntotal += newSal else: prcntRaise = .055 salRaise = num * prcntRaise newSal = num + salRaise salAvg = num / newSal print("Your pay raise percent will be:", format(prcntRaise*100 ,".1f")+'%') print("Your pay raise will be:", format(salRaise, ',.2f'), sep ="") print("Your new salary will be:", format(newSal, ',.2f'), sep ="") ntotal += newSal lineRead = inFile.readline() print("\nTotal Faculty payroll:$", format(total , ",.2f"),sep ="") print("The New Faculty payroll:$", format(ntotal , ",.2f"),sep ="") # Close the file. inFile.close() # Call the main function. main() -------------------------------------------------------------------------- solution Faculty Member:$52500.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,675.00 Your new salary will be:56,175.00 Faculty Member:$64029.50 Your pay raise percent will be: 4.0% Your pay raise will be:$2,561.18 Your new salary will be:66,590.68 Faculty Member:$56000.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,920.00 Your new salary will be:59,920.00 Faculty Member:$50001.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,500.07 Your new salary will be:53,501.07 Total Faculty payroll:$222,530.50 The New Faculty payroll:$236,186.75 Faculty Member:$65500.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,620.00 Your new salary will be:68,120.00 Faculty Member:$42800.00 Your pay raise percent will be: 5.5% Your pay raise will be:2,354.00 Your new salary will be:45,154.00 Faculty Member:$45000.50 Your pay raise percent will be: 5.5% Your pay raise will be:2,475.03 Your new salary will be:47,475.53 Faculty Member:$68900.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,756.00 Your new salary will be:71,656.00 Total Faculty payroll:$444,731.00 The New Faculty payroll:$468,592.28 Faculty Member:$60000.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,400.00 Your new salary will be:62,400.00 Faculty Member:$59999.94 Your pay raise percent will be: 7.0% Your pay raise will be:4,200.00 Your new salary will be:64,199.94 Faculty Member:$54120.25 Your pay raise percent will be: 7.0% Your pay raise will be:3,788.42 Your new salary will be:57,908.67 Faculty Member:$64100.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,564.00 Your new salary will be:66,664.00 Total Faculty payroll:$682,951.19 The New Faculty payroll:$719,764.88 Faculty Member:$44000.50 Your pay raise percent will be: 5.5% Your pay raise will be:2,420.03 Your new salary will be:46,420.53 Faculty Member:$80100.20 Your pay raise percent will be: 4.0% Your pay raise will be:$3,204.01 Your new salary will be:83,304.21 Faculty Member:$90000.00 Your pay raise percent will be: 4.0% Your pay raise will be:$3,600.00 Your new salary will be:93,600.00 Faculty Member:$41000.00 Your pay raise percent will be: 5.5% Your pay raise will be:2,255.00 Your new salary will be:43,255.00 Total Faculty payroll:$938,051.89 The New Faculty payroll:$986,344.62 Faculty Member:$60500.50 Your pay raise percent will be: 4.0% Your pay raise will be:$2,420.02 Your new salary will be:62,920.52 Faculty Member:$72000.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,880.00 Your new salary will be:74,880.00 Faculty Member:$50000.01 Your pay raise percent will be: 7.0% Your pay raise will be:3,500.00 Your new salary will be:53,500.01 Faculty Member:$50000.00 Your pay raise percent will be: 7.0% Your pay raise will be:3,500.00 Your new salary will be:53,500.00 Total Faculty payroll:$1,170,552.40 The New Faculty payroll:$1,231,145.15 Faculty Member:$80001.75 Your pay raise percent will be: 4.0% Your pay raise will be:$3,200.07 Your new salary will be:83,201.82 Faculty Member:$60001.00 Your pay raise percent will be: 4.0% Your pay raise will be:$2,400.04 Your new salary will be:62,401.04 Total Faculty payroll:$1,310,555.15 The New Faculty payroll:$1,376,748.01
You also need to print the total amount of the raises, and to print the average of the raises.
thats the part im having trouble on right now... :(
sum over all the pay_rayses and a counter that adds 1 after the you assign calculate and print at the end of your program
count =0 while lineRead != '': words = lineRead.split() for word in words: num = float(word) count += 1 total += num print("\nFaculty Member:$", format(num, '.2f'), sep ="")
would it be something close to this?
yes, that is right
do i calculate and print it after inFile.close() ?
I would, yes
omg that fix it :D you're the best man!!!!!
Great that you've got it
well..... i made a small mistake :(
i tried to do nr_of_sal += len(words) in my program as for word in words: num = float(word) total += num count += 1 averagePayRaise = total / numOfSal print("\nFaculty Member:$", format(num, '.2f'), sep ="") and it gave me an error
total is the sum of all salaries, not of the pay raise and numOfSal should be count and count needs to be set to zero (initialised) at the top of your main() function
this was my final code :D and it works neatly midSalary = 50000 maxSalary = 60000 def main(): inFile = open('program7.txt', 'r') lineRead = inFile.readline() total = 0.0 ntotal = 0.0 count = 0 while lineRead != '': words = lineRead.split() for word in words: num = float(word) total += num count += 1 print("\nFaculty Member # ",count, ": $" , format(num, '.2f'), sep ="") if num >= maxSalary: prcntRaise = .04 salRaise = num * prcntRaise newSal = num + salRaise print("Pay Raise Percent :", format(prcntRaise*100, ".1f")+'%') print("Pay Raise : $", format(salRaise, ',.2f'), sep ="") print("New Salary : $", format(newSal, ',.2f'), sep = "") ntotal += newSal elif num >= midSalary: prcntRaise = .07 salRaise = num * prcntRaise newSal = num + salRaise print("Pay Raise Percent :", format(prcntRaise*100, ".1f")+'%') print("Pay Raise : $", format(salRaise, ',.2f'), sep ="") print("New Salary : $", format(newSal, ',.2f'), sep = "") ntotal += newSal else: prcntRaise = .055 salRaise = num * prcntRaise newSal = num + salRaise print("Pay Raise Percent :", format(prcntRaise*100, ".1f")+'%') print("Pay Raise : $", format(salRaise, ',.2f'), sep ="") print("New Salary : $", format(newSal, ',.2f'), sep = "") ntotal += newSal lineRead = inFile.readline() averagePayRaise = (ntotal - total) / count # Close the file. inFile.close() for divider in range(45): print("-", end ='') print("\nTotal Faculty payroll : $", format(total , ",.2f"),sep ="") print("The New Total Faculty payroll : $", format(ntotal , ",.2f"),sep ="") print("Average Pay Raise : $", format(averagePayRaise, ",.2f"), sep ="") # Call the main function. main() // thank you so much man. I could've not finished it without you
Nice work, Your intial overall structure was really good
thank you @abtster
Join our real-time social learning platform and learn together with your friends!