function - 如何添加一个参数的多个案例,使其成为代码中某处的变量并将该参数用作函数中的一个
问题描述
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 个班次,但我不知道该怎么做。任何建议都会非常有帮助
解决方案
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
推荐阅读
- java - 我如何从 springboot 中的 src 文件夹外部访问 yaml
- node.js - 使用node js制作带有图像的视频的最佳方法是什么
- javascript - React Element 类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:未定义
- ios - 尝试执行 pod install “[!] FBReactNativeSpec 已添加 1 个脚本阶段”时出现错误。
- javascript - 我该如何使用 setInterval 和 clearInterval ?
- java - 如何通过 Java 中的 JTable 中的默认程序打开文件
- c - 基本条件运算符
- javascript - 如何过滤和映射json文件到html?
- elasticsearch - 过滤日期直方图聚合的输出桶
- r - 如何在 R 的 xml2 包中找到与给定属性名称关联的 xpath