我被我的打印(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
打印的预期结果(tmp[3])为“10”。实际结果。。"10 ". 我不知道为什么它会多打印一行。我认为这个问题的症状是无法计算正常工资=工资率*工作小时数。预期输出为420。42(工资率)*10(小时)=420 下面是第133行的错误“TypeError:不能将序列与第132行的'float'类型的非int相乘” 这是一个家庭作业,我只是想解决这个错误,所以把我的整个程序似乎没有必要
以下是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#如果愿意,可以压缩范围() 将open(fname,'r')作为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) :
regularPayAmount = employeePayRate * employeeHoursWorked
return regularPayAmount
def calculateOvertimePay(employeePayRate, employeeOvertimeHours) :
overtimePayRate = 1.5
overtimePayAmount = (employeePayRate * employeeOvertimeHours) *
overtimePayRate
return overtimePayAmount
def calculateGrossPay(regularPayAmount, overtimePayAmount) :
grossPayAmount = regularPayAmount + overtimePayAmount
return grossPayAmount
def calculateFederalTax(grossPayAmount) :
federalTaxRate = 0.151
federalTaxWithheld = grossPayAmount * federalTaxRate
return federalTaxWithheld
def calculateStateTax(grossPayAmount) :
stateTaxRate = 0.055
stateTaxWithheld = grossPayAmount * stateTaxRate
return stateTaxWithheld
def calculateMedicareTax(grossPayAmount) :
medicareTaxRate = 0.012
medicareTaxWithheld = grossPayAmount * medicareTaxRate
return medicareTaxWithheld
def calculateSocSecTax(grossPayAmount) :
socSecTaxRate = 0.048
socSecTaxWithheld = grossPayAmount * socSecTaxRate
return socSecTaxWithheld
def calculateTotalTaxes(federalTaxWithheld, stateTaxWithheld,
medicareTaxWithheld, socSecTaxWithheld) :
totalTaxesWithheld = federalTaxWithheld + stateTaxWithheld +
medicareTaxWithheld + socSecTaxWithheld
return totalTaxesWithheld
def calculateNetPay(grossPayAmount, totalTaxesWithheld) :
netPayAmount = grossPayAmount - totalTaxesWithheld
return netPayAmount
def payrollSummaryReport(vals):
print()
print("\t\t\t\t\t\tPayroll Summary Report")
print("%-12s%-12s%-8s%-10s%-10s%-12s%-10s%-11s%-13s%-10s" %\
("LastName", "FirstName", "Hours", "RegHours", "OTHours", "RegPay", "OTPay", "GrossPay", "Deductions", "NetPay"))
for i in vals:
print("%-12s%-12s%-8.2f%-10.2f%-10.2f$%-11.2f$%-9.2f$%-10.2f$%-12.2f$%-10.2f" %\
(vals[i]["fname"], vals[i]["lname"], vals[i]["gross"]))
main()
[Expected Output][1]
Payroll Summary Report
姓氏姓名工时常规工时加班工时
高塔马修42.0 40.0 2.0 400.0 30.0 430.00 107.07 322.93
杰克逊·塞缪尔53.040.013.0506.0246.68752.67187.42565.25
琼斯·凯瑟琳35.035.0680.050680.05169.33510.72
薪酬净额合计1398.90
定期工资加班工资总工资扣除净工资
f.readlines
的输出包括行尾(EOL)字符,因此,如果使用逗号拆分行并打印最后一项,它将打印值和行尾字符。如果您执行print(repr(tmp[3]))
,您将看到类似'10\n'
的内容您可以像这样从生产线中剥离EOL:
但是,由于您正在处理csv文件,因此可以使用Python内置的csv模块,它将为您处理一些细节
请注意,从文件读取时
hours
和pay_rate
是字符串。如果要将它们用作数字,则需要使用int或float转换它们,具体取决于所需的数字类型相关问题 更多 >
编程相关推荐