Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

why is the formatting not identical between what is displayed when I execute my

ID: 3850166 • Letter: W

Question

why is the formatting not identical between what is displayed when I execute my code in visual studios and it displays on the console versus what is displayed in the output.txt file? why is the ' ' showing up in the output.text file and why aren't the ID's listed line by line instead of merging together having two different people on the same line at times?

my code is as follows and the above is what I get:

f = open("C:\Usersuser\Desktop\payroll.txt", "r")
outfile = open('C:\Usersuser\Desktop\output.txt','w')
output=[]

for line in f.readlines():
columns=line.split()
id=columns[0]
name=columns[1] + ' ' + columns[2]
wage=float(columns[3])
days=columns[4:]
totalhours=0

for hour in days:
totalhours=totalhours + float(hour)
averageHours=totalhours/len(days)
totalPay=totalhours*wage

result=name+' ID'+id+' worked '+str(totalhours)+' hourly pay $'+str(wage)+' hours: ' + str(averageHours) + '/day Total Pay: $' + str(totalPay)

print(result)
output.append(result+' ')

outfile.write(output.__str__() + ' ')
outfile.close()

c Program Files Python 36pyth On.exe Ben Allen ID0000 worked 21.0 hourly pay $7.0 hours 4.2/day Total Pay: $147.0 dam Faurie ID0001 worked 38.0 hourly pay $18.0 hours 7.6/day Total Pay: $684.0 Chris Delorenzo ID0002 worked 26.0 hourly pay $7.0 hours: 6.5/day Total Pay: $182.0 John Kerns ID0003 worked 40.0 hourly pay $38.0 hours 8.0/day Total Pay: $1520.0 Michael Husserl ID0004 worked 16.0 hourly pay $9.0 hours 4.0/day Total Pay: $144.0 James Black ID0005 worked 26.0 hourly pay $15.0 hours 6.5/day Total Pay: $390.0 Todd Lidner ID0006 worked 29.5 hourly pay $20.0 hours 7.375/day Total Pay: $590.0 Melanie McLaird ID0007 worked 32.0 hourly pay $7.0 hours: 5.333333333333333/day Total Pay: $224.0 Tim Messelt ID0008 worked 31.5 hourly pay $13.0 hours: 6.3/day Total Pay: $409.5 ary Allen ID0009 worked 34.5 hourly pay $25.0 hours: 6.9/day Total Pay: $862.5 Kay Simpson ID0010 worked 40.0 hourly pay $30.0 hours 8.0/day Total Pay: $1200.0 Press any key to continue output Notepad File Edit Format View Help Ben Allen ID0000 worked 21.0 hourly pay $7.0 hours 4.2 Total Pay: $147.0In Adam Faurie ID0001 worked 38.0 hourly pay $18.0 hours 7.6/day Total Pay: $684. 01na', Chris Delorenzo ID0002 worked 26.0 hourly pay $7.0 hours 6. 5/day Total Pay $182.0 John Kerns ID0003 worked 40.0 hourly pay $38.0 hours 8.0/day Total Pay: $1520.0 Michael Husserl ID0004 worked 16.0 hourly pay $9.0 hours 4.0/day Total Pay: $144.0 James Black ID0005 worked 26.0 hourly pay $15.0 hours 6.5/day Total Pay $3900.0 Todd Lidner ID0006 worked 29.5 hourly pay $20.0 hours 7.375/day Total Pay: $590.0 An Melanie McLaird ID0007 worked 32.0 hourly pay $7.0 hours 5.333333333333333 day Total Pay: $224.0 Tim Messelt ID0008 worked 31. 5 hourly pay $13.0 hours 6.3/day Total Pay: $409.5 Mary Allen ID0009 worked 34.5 hourly pay $25.0 hours 6.9/day Total Pay: $862.5 Kay Simpson ID0010 worked 40.0 hourly pay $30.0 hours: 8.0/day Total Pay: $1200.0

Explanation / Answer

1. The issue is with writing list as string to output file.

2. Variable output is of list type, each item is a line of text.

3. Re-write the code such that it iterates over list output and add each line to output file.

__________________________________________________________________________________________

Updated Code:

f = open("C:\Usersuser\Desktop\payroll.txt", "r")
outfile = open('C:\Usersuser\Desktop\output.txt','w')
output=[]

for line in f.readlines():
   columns=line.split()
   id=columns[0]
   name=columns[1] + ' ' + columns[2]
   wage=float(columns[3])
   days=columns[4:]
   totalhours=0

   for hour in days:
        totalhours=totalhours + float(hour)
        averageHours=totalhours/len(days)
        totalPay=totalhours*wage

   result=name+' ID'+id+' worked '+str(totalhours)+' hourly pay $'+str(wage)+' hours: ' + str(averageHours) + '/day Total Pay: $' + str(totalPay)

   print(result)
   output.append(result+' ')

# Iterating over each item in list variable
for line in output:
   # Writing each line to output file
   outfile.write(line)

# Closing output file  
outfile.close()