This is spitting out duplicate data to the output text file instead of just once
ID: 3849643 • Letter: T
Question
This is spitting out duplicate data to the output text file instead of just once...the code above sends the data to an output text file and the code looks like
when it should look like this:
why is it duplicating so much?
payroll.txt file looks like this...
2 f open("C:\Users Desktop Wpayroll.txt "r") 3 outfile open 'C:WUsersWusertMVDesktopWoutput. txt w 4 output 6 or line in f.readlines. columns-line split() id columns [01 name-columns[1 columns[2] float columns [31) 10 Wage days-columns [A:] 11 12 totalhours 0 13 14 for hour in days total hours-total Ours float hour) 1h 15 average Hours totalhours/len (days) 16 17 totalPay totalhours wage 18 result-name. LD +id+' worked '+str(totalhours) hourly pay +str (wage)+' hours: str(averagellours) '/day Total Pay: str (totalpay) 19 20 print (result) 21 output, append(result ') 22 outfile ,write (output str 'Vn') 23 24 25 26 out file close() 27 28 29Explanation / Answer
Line 23 i.e. outfile.write(output +' ') should be outside the for loop.
Right now line 23 is wrting the contents of variable output each time the loop is executing Whereas it should write only once.
So, Line 23 should be outside the for loop.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.