What I'm trying to do is read a line from a file containing some data that includes a person's first and last name, their hourly wage and the number of hours they worked, and then writing to a seperate file their last name, first name: wage.
The input file looks something like this:
First Name 15.00 40.00
New Name 12.75 38.50
The output file should read something like this:
Name, First: $600.00
Name, New: $490.86
Easy peasy, right? Everything seems to be in working order, except, that when it writes to the file, it writes it four times!
def fileInOut():
file_input = open('input.txt', 'r')
file_output = open('output.txt', 'w')
info = file_input.readline()
while info != '':
payroll = info.split()
first = payroll[0]
last = payroll[1]
payrate = float(payroll[2])
hours = float(payroll[3])
wage = payrate*hours
print(last, ',', first, ':', '$', wage)
for line in payroll:
file_output.write(last + ', ')
file_output.write(first + ': ')
file_output.write('${0: .2f}'.format(wage) + '\n')
info = file_input.readline()
file_output.close()
file_input.close()
fileInOut()
Print output is fine:
>>>>
Name, First: $600.00
Name, New: $490.86
>>>>
But the output.txt is
Name, First: $600.00
Name, First: $600.00
Name, First: $600.00
Name, First: $600.00
Name, New: $490.86
Name, New: $490.86
Name, New: $490.86
Name, New: $490.86
Why is it writing four times??????????????