Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting error: "TypeError: 'list' object is not callable" in code

I have a code written up that I believe is finished, except now when I try to run it, I’m getting the following error:

Traceback (most recent call last):
  File "D:/python/salaries.py", line 29, in <module>
    pay = totalManagerPay(salary)
TypeError: 'list' object is not callable

I’ve already done some Googling but every "solution" I find doesn’t seem to work in my particular case, so I figured I’d ask here myself.

My code is here:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

now = datetime.now()
dt_string = now.strftime("%m/%d/%Y %H:%M:%S")

def totalManagerPay(salary):
    return salary

def totalHourlyPay(hours, rate):
    overtime = max(0, hours - 40)
    return (hoursWorked - overtime) * hourlyPay + overtime * hourlyPay * 1.5

def totalCommissionPay(sales):
    return 350 + 0.057 * sales

def totalPieceWorkerPay(pieces):
    bonus = 500 if pieces > 500 else 0
    return pieces * 4.50 + bonus

totalManagerPay, totalHourlyPay, totalCommissionPay, totalPieceWorkerPay = [], [], [], []

while True:
    payCode = int(input("Enter pay code (0 to quit): "))
    if payCode == 0:
        print("No employees at this time. Thank you.")
        break
    if payCode == 1:
        print("MANAGER SELECTED")
        salary = float(input("Enter gross weekly salary: "))
        pay = totalManagerPay(salary)
        totalManagerPay.append(pay)
    elif payCode == 2:
        print("HOURLY WORKER SELECTED")
        hoursWorked = float(input("Enter number of hours worked: "))
        hourlyPay = float(input("Enter hourly rate: "))
        pay = totalHourlyPay(hoursWorked, hourlyPay)
        totalHourlyPay.append(pay)
        print("Hourly worker's pay is ${pay:.2f}\n")
    elif payCode == 3:
        print("COMMISSION WORKER SELECTED")
        weeklySales = float(input("Enter weekly sales: "))
        pay = totalCommissionPay(weeklySales)
        totalCommissionPay.append(pay)
        print("Commission worker's pay is ${pay:.2f}\n")
    elif payCode == 4:
        print("PIECE BY PIECE WORKER SELECTED")
        piecesProduced = int(input("Enter number of pieces produced: "))
        pay = totalPieceWorkerPay(piecesProduced)
        totalPieceWorkerPay.append(pay)
        print("Piece by Piece worker's pay is ${pay:.2f}\n")
    else:
        print("Error. Please enter a valid code.")
        
if any([totalManagerPay, totalHourlyPay, totalCommissionPay, totalPieceWorkerPay]):

    print("\n******************** ABCD Inc. ************************")
    print("*********** Cumulative Salaries By Pay Code ***********")
    print("Employee Pay Code      Number of Employees     Cumulative Salary")
    print("-----------------------------------------------------------------------------")

    totalSalary, totalEmployees = 0, 0
    
    for payCode, payList in enumerate([totalManagerPay, totalHourlyPay, totalCommissionPay, totalPieceWorkerPay], start=1):
        total = sum(payList)
        totalSalary += total
        totalEmployees += len(payList)
        print(f"        {payCode:<25}{len(payList):<25}{total:.2f}")
        
    print("------------------------------------------------------------------------------")
    print(f"Totals                           {totalEmployees:<34}  {totalSalary:.2f}")
    print("--------------------------------------------------------------------------------")
    print("Date: " + dt_string)
    print("************************************************************************")

I thought the error would be when I call the functions in the bottom if-statement, however, when I remove the parentheses, I still get an error.

>Solution :

You are defining lists with same name as your functions. Here:

totalManagerPay, totalHourlyPay, totalCommissionPay, totalPieceWorkerPay = [], [], [], []

is same as

def totalManagerPay(salary):
    return salary

def totalHourlyPay(hours, rate):
    overtime = max(0, hours - 40)
    return (hoursWorked - overtime) * hourlyPay + overtime * hourlyPay * 1.5

def totalCommissionPay(sales):
    return 350 + 0.057 * sales

def totalPieceWorkerPay(pieces):
    bonus = 500 if pieces > 500 else 0
    return pieces * 4.50 + bonus

Change either of them to another name will help. Do remember to modify the code where you call the functions or append the lists accordingly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading