How to use .append to make a constant list in a loop

时间:2019-04-17 00:35:37

标签: python python-3.x

So my current snippet of code is running properly, but I want to create a list where I could store all the payOut (variable)

so if:

wage = [10 , 5, 4] and addedHours = [2 , 3, 2]

the new variable I want (lets call it totalWage):

totalWage = [20, 15, 9]

I want this totalWage variable to be part of the for loop in the code below. How would I do that?

    def printPayroll(self):
        totalPayroll = 0
        i = 0
        product = ""
        for y in names:

            payOut = float(wage[i]) * float(addedHours[i])
            totalPayroll += payOut
            product += ('%-10s%-10s%-0s%-0s') % (str(names[i]), str(addedHours[i]), str(payOut), "\n")

            i += 1
        finalPayroll = "Total Payroll =    $" + str(totalPayroll)

3 个答案:

答案 0 :(得分:0)

def printPayroll(self):
    totalPayroll = 0
    i = 0
    product = ""
    totalWage = []

    for y in names:
        payOut = float(wage[i]) * float(addedHours[i])
        totalPayroll += payOut
        product += ('%-10s%-10s%-0s%-0s') % (str(names[i]), str(addedHours[i]), str(payOut), "\n")

        i += 1
        totalWage.append(payOut)

    finalPayroll = "Total Payroll =    $" + str(totalPayroll)
    print(totalWage)

ps. Computation of payroll and print of it can be separated for better code. Also for loop can be simplified with zip function. I am not including because it is not part of the question.

答案 1 :(得分:0)

You just need to define a totalWage list, and append your wage*addedHours for each item in that list. In addition, you can use enumerate to both get the indexes and the item in a list.

wage = [10 , 5, 4]
addedHours = [2 , 3, 2]
names = ['Jack', 'John', 'Joe']
def printPayroll():
    totalWage = []
    totalPayroll = 0
    i = 0
    product = ""
    for i, name in enumerate(names):

        payOut = float(wage[i]) * float(addedHours[i])
        totalWage.append(payOut)
        totalPayroll+= payOut
        product += ('%-10s%-10s%-0s%-0s') % (name, str(addedHours[i]), str(payOut), "\n")

    finalPayroll = "Total Payroll =    $" + str(totalPayroll)
    print(totalWage)
    print(product)
    print(finalPayroll)

printPayroll()
#[20.0, 15.0, 8.0]
#Jack      2         20.0
#John      3         15.0
#Joe       2         8.0

#Total Payroll =    $43.0

答案 2 :(得分:0)

You need to declare an empty list within function and keep appending every value of payout inside the loop. You can also use this faster if you have many rows for computation using zip & use pandas dataframe to store result in case you want to later write the output to csv and store for future references. Here is the code:

wage = [10 , 5, 4]
addedHours = [2 , 3, 2]
import pandas as pd
def printPayroll():
   totalPayroll = 0
   totalWage = []
   for w, h in zip(wage, addedHours):
      payout = w*h
      totalPayroll = totalPayroll + payout
      totalWage.append(payout)
   print("Total Payroll =    $" + str(totalPayroll))
   print("Total wage:" , totalWage)
   # if names are A, B, C
   names = ["A", "B", "C"]
   print(pd.DataFrame({'name':names, 'added_hours': addedHours, 'total_wage': totalWage}))

printPayroll()

Hope this helps!