我对我的项目的print(tmp [3])函数感到困惑。当打印项目tmp [3]时,它给了我两行,其中第一行带有值,另一行为空。一切顺利进行到第132行。
该程序的目标是打印一个。工资单摘要报告基于先前程序的信息(具有不同的费率)。您应该使用单个功能来执行以下操作:
[计算正常工作时间(40小时及以下)计算加班时间(超过40小时的加班时间)计算常规工资(常规时间乘以常规工资)计算加班工资(加班时间乘以正常工资乘以1.5)计算总工资计算预扣的联邦税额(占总工资的15.1%)计算预扣的州税金额(占总工资的5.5%)计算被扣留的医疗保险金额(占总工资的1.2%)计算扣留的社会保障金额(占总工资的4.8%)计算扣除总额(联邦税+国家税+医疗保险+社会保障)计算净工资(总工资-扣除额)计算净额(每个人的总计)打印(到屏幕上)干净的摘要报告(请参见上面的输出)打印(在屏幕上)总净工资。
我找到了基于输入的版本的功能,因此,从csv中提取数据并在功能中使用它对我来说是一个挑战。
如果您想要更简单的复制和以前类似的程序,这里是链接:Read records from CSV file and print report
print(tmp [3])的预期结果是“ 10”。实际结果。“ 10”。我不知道为什么它会额外打印一行。我认为这个问题的症状是无法计算正常工资= Payrate * hoursWorked。预期输出为420。42(支付率)* 10(小时)= 420这是第133行的错误“ TypeError:无法将序列乘以'float'类型的非整数”这是一项家庭作业,我只是想解决这个错误,因此放置我的整个程序似乎是不必要的。
这里是csv文件:
First, Last, Hours, Pay
Matthew, Hightower, 42, 10
Samuel, Jackson, 53, 12.58
Catherine, Jones, 35, 19.43
这是整个程序。def main():
results = get_data("employeestest.csv")
payrollSummaryReport(results)
def get_data(fname):
Function returns the dictionary with following
format:
{ 0 : {
"fname": "...",
"lname": "...",
"gross": "...",
},
1 : {
....,
,,,,
},
}
结果= {}#返回值i = 0#如果您想压缩range()用open(fname,'r')as f:
for line in f.readlines()[1:]:
result[i] = {}
tmp = line.split(",") # list of values from file
# access file values by their index, e.g.
# tmp[0] -> first name
# tmp[1] -> last name
# tmp[2] -> hours
# tmp[3] -> pay rate
employeeRegularHours, employeeOvertimeHours = calculateRegularHours(tmp[2])
employeeOvertimeHours = calculateOvertimeHours(tmp[2])
employeeTotalHours = calculateTotalHours(employeeRegularHours, employeeOvertimeHours)
print(tmp[3])
This is were I print the item but it comes out to
"10
"
I don't know why it skips a line. I found this error through this "TypeError:can't multiply sequence by non-int of type 'float'" at line 132
#print(employeeRegularHours)
regularPayAmount = calculateRegularPay(tmp[3], employeeRegularHours)
overtimePayAmount = calculateOvertimePay(tmp[3], employeeOvertimeHours)
grossPayAmount = calculateGrossPay(regularPayAmount, overtimePayAmount)
federalTaxWithheld = calculateFederalTax(grossPayAmount)
stateTaxWithheld = calculateStateTax(grossPayAmount)
medicareTaxWithheld = calculateMedicareTax(grossPayAmount)
socSecTaxWithheld = calculateSocSecTax(grossPayAmount)
totalTaxesWithheld = calculateTotalTaxes(federalTaxWithheld, stateTaxWithheld,
medicareTaxWithheld, socSecTaxWithheld)
netPayAmount = calculateNetPay(grossPayAmount, totalTaxesWithheld)
#(calculateOvertimePay, calculateTotalHours) =
#etc.) and store the results in dictionary
# e.g:
result[i]["fname"] = tmp[0]
result[i]["lname"] = tmp[1]
result[i]["hours"] = tmp[2]
result[i]["payrate"] = tmp[3]
# ...
# do calculations for report
# ...
result[i]["regular"] = employeeRegularHours
result[i]["overtime"] = employeeOvertimeHours
result[i]["totalhours"] = employeeTotalHours
result[i]["regPay"] = regularPayAmount
result[i]["overPay"] = overtimePayAmount
result[i]["gross"] = grossPayAmount
result[i]["fedtax"] = federalTaxWithheld
result[i]["stateTax"] = stateTaxWithheld
result[i]["medTax"] = medicareTaxWithheld
result[i]["socsectax"] = socSecTaxWithheld
result[i]["totaltax"] = totalTaxesWithheld
result[i]["netpay"] = netPayAmount
i += 1
return result
def calculateRegularHours(employeeHoursWorked) :
#print(employeeHoursWorked)
if float(employeeHoursWorked) < 40.0 :
employeeRegularHours = employeeHoursWorked
employeeOvertimeHours = 0.0
else:
employeeRegularHours = 40.0
employeeOvertimeHours = 0.0
#employeeOvertimeHours = employeeHoursWorked - 40.0
return employeeRegularHours, employeeOvertimeHours
def calculateOvertimeHours(employeeHoursWorked) :
if float(employeeHoursWorked) > 40 :
#float(employeeOvertimeHours) = employeeHoursWorked - 40
#print(employeeHoursWorked)
employeeOvertimeHours = 0.0
else :
employeeOvertimeHours = 0
return employeeOvertimeHours
def calculateTotalHours(employeeRegularHours, employeeOvertimeHours) :
employeeTotalHours = employeeRegularHours #+ employeeOvertimeHours
return employeeTotalHours
def calculateRegularPay(employeePayRate, employeeHoursWorked) :
print(employeeHoursWorked)
print(employeePayRate)
regularPayAmount = employeePayRate * employeeHoursWorked# LINE 132 It trys to multiple the ten with #the pay rate
return regularPayAmount
[Expected Output][1]
Payroll Summary Report
姓氏名称小时数正常时间加班时间高层马太福音42.0 40.0 2.0 400.0 30.0 430.00 107.07 322.93杰克逊·塞缪尔53.0 40.0 13.0 506.0 246.68 752.67 187.42 565.25琼斯·凯瑟琳35.0 35.0 0.0 680.05 0.0 680.05 169.33 510.72
总净工资1,398.90
常规工资OT工资总工资扣除净工资
f.readlines
的输出包括行尾(EOL)字符,因此,如果在逗号上分割行并打印最后一个项目,它将在行尾字符上打印值and。如果您执行print(repr(tmp[3]))
,将会看到类似'10\n'
的内容。
您可以像这样从行中删除EOL:
tmp = line.strip().split(",")
但是,由于您正在处理csv文件,因此可以使用Python的内置csv模块,该模块将为您处理一些详细信息
import csv
# Do stuff
with open(fname, 'r', newline='') as f:
reader = csv.reader(f)
# Skip header row
next(reader)
# reader yield lists, but since we know each list will have
# the same number of elements we can unpack them into
# four separate variables - this is more readable than
# referencing by index.
for first_name, last_name, hours, pay_rate in reader:
employeeRegularHours, employeeOvertimeHours = calculateRegularHours(hours)
employeeOvertimeHours = calculateOvertimeHours(hours)
employeeTotalHours = calculateTotalHours(employeeRegularHours, employeeOvertimeHours)
# Do more stuff
请注意,从文件中读取时hours
和pay_rate
是字符串。如果要将它们用作数字,则需要根据所需的数字类型使用int或float进行转换。