In PYTHON:Implement a function payroll() that takes two string parameters the fi
ID: 3811700 • Letter: I
Question
In PYTHON:Implement a function payroll() that takes two string parameters the first of which
represents the name of an input file and the second of which represents the name of an
output file. The input file contains lines with the following format: hours pay_rate where
hours is an integer representing a number of hours worked and pay_rate is a floating
point value representing the pay rate per hour. There will be some whitespace between
the two values, although you may not make any assumptions about how much or what
type. The function reads the input file line by line, computing the total pay (i.e. the
product of the hours times the pay_rate -- we are not assuming overtime is computed for
any of the values) and writing each amount to a newline in the output file. If the file is
empty, the function should not write any numbers to the output file. Note that the strings
read from the file will need to be evaluated before you can use them for
computations. The following shows what the function would return when called on
several sample files found in the zip file containing the assignment template. The output
files produced are also in the zip file:
Python 341 Shel File Edit Shell Debug Options Windows Help payroll pay1.txt', out 1.txt fl open( out1.txt 'r') print (fl.read()) 157.5 1006.0 116.25 245.0 fl,close payroll pay 2.txt', out 2.txt f2 open out2.txt', 'r') print (f2. Dead()) 878.75 578.0 367.0 310.0 116.25 180.0 474.25 446.25000000000006 f2 close payroll pay 3.txt tout 3,txt f3 open ('out3 .txt', 'r') print (f3 read()) f3 closeExplanation / Answer
The code for above problem using python is given as:
#function to read and to write in file
def payroll(str1,str2):
#open files to read and write
file = open(str1, 'r')#in read mode
file1 = open(str2, 'w')#in write mode
for line in file:#to read file line by line
wordList = []#initialize empty array to read word in a line
for word in line.split():
#split line on the basis of white character
wordList.append(word) #adding words in List
result=float(wordList[0])*float(wordList[1])
#now you know that there will be 2 words only one line.So using index 0 and 1
file1.write(str(result)+" ")
#writing output in output file
file1.close()
file.close()
payroll("pay1.txt","out1.txt")#calling function
f1=open("out1.txt",'r')#reading output file
print(f1.read())
f1.close()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.