列表的一个索引变成另一个列表,而其他索引保持不变?

我目前正在尝试读取3个文件,这些文件分别代表员工,费率和工作时间列表。由于某种原因,我的工作时间清单出现了问题。我正在尝试将日志记录插入程序中,以告诉这些列表中是否有一个错误的值类型(即小时列表中的一个单词),同时让程序的其余部分运行。当我在小时表中添加了第三个指数超过80(这是提醒潜在的时间表错误/欺诈的重要条件)时,它开始将此数字拆分成自己的列表。

我尝试将数字降低到两位数,因为我最初以荒谬的1000开始。然后,我尝试更改它显示日志的方式,因为我认为可能是问题所在。

这就是我要提取文件并将其放入单个列表的方式

Employeesl=[]#sets a list for employees
hrRatel=[]#sets a list for hourly pay rates
hrWorkl=[]#sets a list for hours worked
Employees=open('C:\\MGMT28800\\names.txt') #sets a variable for employees
hrRate=open('C:\\MGMT28800\\rates.txt') #sets a variable for hourly pay rates
hrWork=open('C:\\MGMT28800\\hours.txt') #sets a variable for hours worked
Employeesc=Employees.read()#sets variable to the file
hrRatec=hrRate.read()#sets variable to the file
hrWorkc=hrWork.read()#sets variable to the file
Employeesl=Employeesc.split('\n')#splits the file data into a list
hrRatel=hrRatec.split('\n')#splits the file data into a list
hrWorkl=hrWorkc.split('\n')#splits the file data into a list

这是我的功能,用于计算工资和工作时间是否太长

def payCalc(hrRatel,hrWorkl):#create function
        logging.debug('Start of payCalc for iteration (%s)' % (i))
        if hrWorkl[i]<=40:#tells payment for employees with equal to and sub 40 work hours
            z=hrWorkl[i]*hrRatel[i]#equation to solve for payment
            return z
            logging.debug('End of payCalc for iteration (%s)' % (i))
        elif 40<hrWorkl[i]<=80:#payment for those with more than 40 hrs. accounts for overtime,but restricts more than 80 hrs of work per period
            x=hrWorkl[i]#sets a variable equal to employees work time
            a=x-40# finds how many hours overtime worked
            opay=a*1.5*hrRatel[i]# figures out overtime payment
            b=hrWorkl[i]-a#sets the 40 hours not overtime
            pay=b*hrRatel[i]#calculates the 40 hours of pay at the base rate
            z=pay+opay#adds base and overtime pay
            logging.debug('You will pay '+str(z)+' to '+Employeesl[i]+'.')
            return z
            logging.debug('End of payCalc for iteration (%s)' % (i))
        else:
            raise Exception(Employeesl[i]+' has worked more than 80 hours,totaling to '+str(hrWorkl[i])+'.')
            logging.debug('End of payCalc for iteration (%s)' % (i))

这是我调用函数的地方,并检查列表中是否存在不兼容的值类型以供其调用错误。

for i in range(len(Employeesl)):#sets a loop to print out the employee follwoed by their specific pay for the pay period
    try:
        if hrRatel[i]==str:
            raise Exception('The rate ('+str(hrRatel[i])+') must be a number')
        else:
            hrRatel[i]=map(float,hrRatel[i])#ensures all rates are floated
        if hrWorkl[i]==str:
            raise Exception('The hours worked ('+str(hrWorkl)+') must be a number')
        else:
            hrWorkl[i]=map(int,hrWorkl[i])#ensures all rates are numbers
        z=payCalc(hrRatel,hrWorkl)#calls the payCalc() function
        Total=Total+z#provides a count of all the salaries added up

    except Exception as err:
        print('An error has occured,'+str(err))

我希望它能告诉我fabio fakerson已经工作了80多个小时,无论文件说了80多个小时都工作了。相反,当我让他工作81个小时时,它输出:发生了错误,fabio fakerson已经工作了80多个小时,总计[8,1]。

在将字符串转换为浮点数时,还会出现两个错误,但据我所知,这种情况应该不会发生

发生错误,无法将字符串转换为float:。 发生错误,无法将字符串转换为float:。

但是,主要问题是81已转换为列表,并且似乎并没有将每个雇员的工资加起来。

csx80088 回答:列表的一个索引变成另一个列表,而其他索引保持不变?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3157389.html

大家都在问