Andrew Andrew - 2 years ago 65
Python Question

contradictory output while calling two defined functions at one time

Stuck up in confusion!! observe the the function 1st:
(please ignore the intended space which is not inside the function body as i am trying to edit to add the space but failed, well thats apart of the problm, ignore that)

def tax(amt):
if amt < 241:
r = 0
elif amt < 481:
r = amt * 0.15
r = amt * 0.28
return r

def net(amt):
return amt - tax(amt)

now when i call these only one function at one time like given below, then the output comes out successfully without any error:

print('enter The Grosspay')
r = int(raw_input())
net_pay = net(r)

But when i call both function together like given below then the error pops up with a message that "float object is not callable"

print('enter The Grosspay')
r = int(raw_input())
tax = tax(r)
net_pay = net(r)
print("tax: " + tax + "\t" + "net payment: " + net_pay)

Answer Source

You can't have the same name represent both the function and the float.

This line creates the global name tax and binds it to a function:

def tax(amt):

This line creates the global name tax and binds it to a float:


This second binding (to float) destroys the first binding (to a function). Later, when net calls tax(), you'll get the error "float object is not callable".

Solution: Give your functions and your variables different names.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download