首页 > 解决方案 > 如何添加一个参数的多个案例,使其成为代码中某处的变量并将该参数用作函数中的一个

问题描述

    import csv

    def Calc(process, shift, lc):
        f = open('C:\\Users\\keshabg\\Desktop\\sql_testing\\table_1.csv')
        csv_f = csv.reader(f)
        res_units = []
        res_hours = []
        sum_units = 0
        sum_hours = 0
        for row in csv_f:
            if row[2] == shift and row[3] == process and row[4] == lc:
                res_units.append(row[5])
                res_hours.append(row[6])
       for num in res_units:
           sum_units = float(sum_units) + float(num)
       for num in res_hours:
           sum_hours = float(sum_hours) + float(num)
       try:
           res_rate = round(sum_units/sum_hours, 2)
       except ZeroDivisionError:
           res_rate = "N/A"

       return "The Units Processed was: ", res_units , "The Hours worked was: " , res_hours , "The total units was: " , sum_units , "The total hours was: " , sum_hours , "The rate: " , res_rate

嘿伙计们,我的代码在函数中传递过程、班次和学习曲线级别参数后给出了速率。有 7 个班次,现在我想做的是,我想做 A 到 E 的白班和 F 到 G 的夜班,总共 9 个班次,但我不知道该怎么做。任何建议都会非常有帮助

在此处输入图像描述

标签: functioncsv

解决方案


    import csv

    def Calc(process, shift, lc):
        if shift == 'day':
            shift = ['A','B','C','D','E']
        if shift == 'night':
            shift = ['F','G']
        f = open('C:\\Users\\keshabg\\Desktop\\sql_testing\\table_1.csv')
        csv_f = csv.reader(f)
        res_units = []
        res_hours = []
        sum_units = 0
        sum_hours = 0
        for row in csv_f:
            if row[2] in shift and row[3] == process and row[4] == lc:
                res_units.append(row[5])
                res_hours.append(row[6])
        for num in res_units:
            sum_units = float(sum_units) + float(num)
        for num in res_hours:
            sum_hours = float(sum_hours) + float(num)
        try:
            res_rate = round(sum_units/sum_hours, 2)
        except ZeroDivisionError:
            res_rate = "N/A"

        return "The Units Processed was: ", res_units , "The Hours worked was: " , res_hours , "The total units was: " , sum_units , "The total hours was: " , sum_hours , "The rate: " , res_rate 

推荐阅读