# 在遍历CSV文件时需要帮助。仅输出了工资单摘要的一名员工

• Compute the regular hours worked (40 hours and below)
• Compute the overtime hours worked (those above 40 hours)
• Compute the regular pay (regular hours times regular pay)
• Compute the overtime pay (overtime hours times regular pay times 1.5)
• Compute the gross pay
• Compute the amount of federal tax withheld (15.1% of gross pay)
• Compute the amount of state tax withheld (5.5% of gross pay)
• Compute the amount of medicare withheld (1.2% of gross pay)
• Compute the amount of social security withheld (4.8% of gross pay)
• Compute the total deductions (federal tax + state tax + medicare + social security)
• Compute the net pay (gross pay - deductions)
• Compute the total (total for everyone) net pay
• Print (to the screen) a clean summary report (see the output above)
• Print (to the screen) the total net pay
• Expected out put is: Or Ideal output

Better Actual output Image

CSV文件

``````First,Last,Hours,Pay
Matthew,Hightower,42,10
Samuel,Jackson,53,12.65
Catherine,Jones,35,19.43
Charlton,Heston,52,10
Karen,Black,40,12
Sid,Caesar,38,15
George,Kennedy,25,35
Linda,Blair,42,18.6
Beverly,Garland,63,10
Jerry,Stiller,52,15
Efrem,Zimbalist,34,16
Linda,Harrison,24,14
Myrna,Loy,40,14.23
``````

``````import csv

def main():

results = get_data("employeestest.csv")

payrollSummaryReport(results)
print(results)
def get_data(fname):

results = {} # return value
with open(fname, 'r', newline='') as f:

result = {} # will be used to grab all of the results then return them to the last function
# 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:

hours = float(hours)
pay_rate = float(pay_rate)

#Calculatations
employeeRegularHours, employeeOvertimeHours = calculateRegularHours(hours)
employeeOvertimeHours = calculateOvertimeHours(hours)
regularPayAmount = calculateRegularPay(pay_rate, employeeRegularHours)
overtimePayAmount = calculateOvertimePay(pay_rate, employeeOvertimeHours)
grossPayAmount = calculateGrossPay(regularPayAmount, overtimePayAmount)
federalTaxWithheld = calculateFederalTax(grossPayAmount)
stateTaxWithheld = calculateStateTax(grossPayAmount)
medicareTaxWithheld = calculateMedicareTax(grossPayAmount)
= calculateSocSecTax(grossPayAmount)
totalTaxesWithheld = calculateTotalTaxes(federalTaxWithheld, stateTaxWithheld,       medicareTaxWithheld, socSecTaxWithheld)
netPayAmount = calculateNetPay(grossPayAmount, totalTaxesWithheld)

result["LastName"] = last_name
result["FirstName"] = first_name
result["Hours"] = hours
result["RegHours"] = employeeRegularHours
result["OTHours"] = employeeOvertimeHours
result["RegPay"] = regularPayAmount
result["OTPay"] = overtimePayAmount
result["GrossPay"] = grossPayAmount
result["Deductions"] = totalTaxesWithheld
result["NetPay"] = netPayAmount

return result

def calculateRegularHours(employeeHoursWorked) :
#print(employeeHoursWorked)

if 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 employeeHoursWorked > 40.0 :
employeeOvertimeHours = employeeHoursWorked - 40.0

employeeOvertimeHours = 0.0
else :
employeeOvertimeHours = 0.0

return employeeOvertimeHours

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

def calculateNetPay(grossPayAmount, totalTaxesWithheld) :
netPayAmount = grossPayAmount - totalTaxesWithheld
return netPayAmount

def payrollSummaryReport(vals):
print()
print("\t\t\t\t\t\tPayroll Summary Report")
print()
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["LastName"], vals["FirstName"], vals["Hours"], vals["RegHours"], vals["OTHours"], vals["RegPay"], vals["OTPay"], vals["GrossPay"], vals["Deductions"], vals["NetPay"]))
return vals
main()

#print('Total Net Pay')
#print('\t\t \$%.2f' % netPayAmount)
``````