TypeError: 'float' object is not callable in Python 3

Dung Do Tien Apr 26 2022 391

I'm just joining to learn Python from a course on Youtube, and today I learn about functional in Python and I have created a small function to help calculate the salary for employees, like this:

 import numpy as np

def calculateSalary(days , salary, offday) :
  return (days - offday) * salary

text = "Total your salary for this month is: $"
calculateSalary = calculateSalary(24, 100.23, 2)

print(text, str(calculateSalary))

text2 = "Total your salary for this month is: $"
calculateSalary = calculateSalary(24, 130.23, 0)
print(text2, str(calculateSalary))

But When I was running the code above and got an exception TypeError: 'float' object is not callable.

 Traceback (most recent call last):
  File "main.py", line 12, in <module>
    calculateSalary = calculateSalary(24, 130.23, 0)
TypeError: 'float' object is not callable

But when I tried to remove the three last lines of code it worked for me, like this:

 import numpy as np

def calculateSalary(days , salary, offday) :
  return (days - offday) * salary

text = "Total your salary for this month is: $"
calculateSalary = calculateSalary(24, 100.23, 2)

print(text, str(calculateSalary))

It really worked for me. Anyone can help me explain it?

I'm using Python 3.9 and Windows 11.

Thanks in Advance.

Have 1 answer(s) found.
  • D

    Devx Mon Apr 26 2022

    This error occurs because your variable is same with your method (it's calculateSalary).

    You can change your variables to another name, see below:

     def calculateSalary(days , salary, offday) :
      return (days - offday) * salary
    
    text = "Total your salary for this month is: $"
    calculateSalary_1 = calculateSalary(24, 100.23, 2)
    
    print(text, str(calculateSalary_1))
    
    text2 = "Total your salary for this month is: $"
    calculateSalary_2 = calculateSalary(24, 130.23, 0)
    print(text2, str(calculateSalary_2))

    #Output

     Total your salary for this month is: $ 2205.06
    Total your salary for this month is: $ 3125.5199999999995

    I hope it is useful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close