Looping over a text file list with python

后端 未结 5 1466
予麋鹿
予麋鹿 2021-01-29 10:16

EDIT: Have updated post for better clarity, no answers have yet to help!

Alright, so my assignment is to take a text file, that would have 4 entries per line, those bein

5条回答
  •  既然无缘
    2021-01-29 10:49

    try these changes:

    totothrs = 0
    totgross = 0.0
    employees = 0
    for tmp in data:
        employees += 1
        fname, lname, rate, hrs = tm.split()
        hrs = int(hrs)
        rate = float(rate)
        othrs = 0
        if hrs > 40:
            othrs = hrs - 40
            hrs = hrs - othrs
    
        totothrs += othrs
        gross = rate * hrs + (1.5*rate)*othrs
        totgross += gross
        heading3= "{0:15s}{1:15s}{2:2d}{3:10d}{4:14d}   {5:24.2f}".format(firstName, lastName, hrs, rate, othrs, gross)
        print heading3
    
    spaceHeading = "       "
    heading4= "{0:15s}{1:21.2f}".format("Total Gross Pay", totgross)
    heading5= "{0:15s}{1:19.2f}".format("Average Gross Pay", (totgross/employees)
    heading6= "{0:15s}{1:16d}".format("Total Overtime Hours", totothrs)
    
    print heading4
    print heading5
    print heading6
    

    Note: you dontneed to define the "headingN"'s you can just print them

提交回复
热议问题