0

I'm trying to figure out why the output to the console and the output to the output.txt file only displays one line of the database entries from my original text file when there are ten lines. It works correctly for that single line and the math is right but I need the output to display all the data at once. Any suggestions?

f = open("C:\\Users\\myname\\Desktop\\payroll.txt", "r")
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+'\n')

outfile=open('C:\\Users\\myname\\Desktop\\output.txt','w')
outfile.writelines(output)
outfile.close()
dazedandconfused
  • 33
  • 2
  • 2
  • 7

1 Answers1

0

In the first for loop, you are overwriting the columns variable with each iteration.

I think you are viewing columns as a "vertical array" that contains all the ids, names, etc in the file. That's a misunderstanding. column just contains the id, name, etc of the specific line in the file where the iterator is at. In the next iteration, the old values are replaced by the next line's values.

I would recommend cramming all the code(splitting, naming, calculations, and writing to file) in a single for loop. You could cram it all inside the top for loop. Something like this:

**Output is ugly, format it as you preferr

f = open("payroll.txt", "r")
outfile = open('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+'\n')
     outfile.write(output.__str__() + '\n')

outfile.close()
g.o.a.t.
  • 420
  • 4
  • 13
  • the output is on a single horizontal line. so, I would delete the second for loop and combine it with the first? – dazedandconfused Jun 15 '17 at 18:31
  • dumb question but do you know how I can round the averageHours variable so it only reads two decimal places out instead of going on for quite a bit? – dazedandconfused Jun 16 '17 at 17:17
  • Also, the output text file repeats many of the lines of data over and over when there should only be like 10 lines of data – dazedandconfused Jun 16 '17 at 17:24
  • It doesn't repeat on my computer. Did you copy and paste the code? Try it, only changing the input file name. To round decimals check https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points – g.o.a.t. Jun 16 '17 at 18:51
  • yeah my output text file, while using visual studios 17 or eclipse for Python comes out crazy even after copying and pasting directly from your code...it looks like : – dazedandconfused Jun 16 '17 at 19:25
  • as a sample...['Ben Allen ID0000 worked 21.0 hourly pay $7.0 hours: 4.2/day Total Pay: $147.0\n'] ['Ben Allen ID0000 worked 21.0 hourly pay $7.0 hours: 4.2/day Total Pay: $147.0\n', 'Adam Faurie ID0001 worked 38.0 hourly pay $18.0 hours: 7.6/day Total Pay: $684.0\n'] ['Ben Allen ID0000 worked 21.0 hourly pay $7.0 hours: 4.2/day Total Pay: $147.0\n', 'Adam Faurie ID0001 worked 38.0 hourly pay $18.0 hours: 7.6/day Total Pay: $684.0\n', 'Chris Delorenzo ID0002 worked 26.0 hourly pay $7.0 hours: 6.5/day Total Pay: $182.0\n'] – dazedandconfused Jun 16 '17 at 19:26
  • are you sure it is not an issue with the input file? – g.o.a.t. Jun 16 '17 at 20:11
  • I'm just using notepad and entering it like 0001 Ben Allen 7 5 5 3 6 2 0002 Adam Faurie 18 5 8 9 8 8.. etc... up till 10 lines. one row at a time per person. – dazedandconfused Jun 19 '17 at 00:27