我有:
for emp_id,pay_rate,job_num1,hours_worked1,job_numb2,hours_worked2,job_num3,hours_worked3,job_numb4,hours_worked4,job_numb5,hours_worked5 in master_list:
print('Job Number:', job_num1, 'Employee Id:', emp_id, 'Hours Worked:', hours_worked1, 'Hourly Rate:', pay_rate, 'Gross Pay:', int(pay_rate) * int(hours_worked1), sep ='')
此循环从嵌套循环中提取值。 我得到了:
Job Number:963789Employee Id:001Hours Worked:40
Hourly Rate:15
Gross Pay:600
...等
为什么每小时费率和总薪水会出现在新线路上?它们不应该作为整个单行的一部分出现吗?我只是在寻找一个简单的解决方案
答案 0 :(得分:0)
这是另一种使用字符串格式的方法,也是一些逻辑,因为你拥有的每条记录都包含三个项目。
s = 'Job Number:{0} Employee Id: {1} Hours Worked: {2} Hourly Rate: {3} Gross Pay: {4}'
record_size = 3 # Each record consists of three items
for record in master_list:
record = map(str.strip, record) # Removes whitespace
for i in xrange(0, len(record), record_size):
bits = record[i:i+record_size]
jnum, empid, hours, rate = bits
gross_pay = int(rate) * int(hours)
print(s.format(jnum, empid, hours, rate, gross_pay), sep='')