[Tutor] float object not callable error
Bob Gailer
bgailer at alum.rpi.edu
Thu Oct 12 21:24:38 CEST 2006
Kristinn Didriksson wrote:
> Hello,
> I an completely new to programming and am trying to teach myself
> Python using Python Programming by John Zelle. Essentially I am an ex-
> tech writer trying to learn how to program.
> My system: Mac OS X 10.4, Python 2.5, TextMate editor
> Here is the problem:
>
> I get 'float' object not callable error.
>
Please in future posts include the exception message (traceback). It
pinpoints the line of code raising the exception. You did help us here
by commenting the problem lines.
> I thought this was a conversion error, but it seems to me that it may
> be something else. It will not print x and I don't understand why it
> will not do that. Any help is greatly appreciated.
>
> This is the code below. When I got the error message initially, I
> tried to break it down into smaller bites so I could see the problem
> more clearly. I tried to figure this out myself, but not having any
> luck.
>
> Regards,
> Kristinn
>
> # Calculate the area of a triangle. Input the lengths of the sides:
> a, b, c.
> # Formulas: (s = a + b + c) / 2
> # A = sqrt(s(s - a)(s -b)(s - c))
>
> def main():
> import math
> a, b, c = input("Please enter the sides of the triangle separated by
> commas: ")
> s = (float(a + b + c)/2)
> print s #This is to test the first part of the algorithm--and it fails
> x = (s(s -a)(s -b)(s -c))
>
In algebra multiplication is assumed when 2 items are adjacent. In most
programming languages multiplication is expressed by some symbol. Python
(as many languages) uses * for multiplication. Parentheses following a
name assumes the name is a callable object (e.g. function). So change
the above to:
x = s*(s -a)*(s -b)*(s -c)
Note I've also dropped the outside parentheses as (1) not necessary and
(2) potentially confusing considering that outside parentheses are used
to construct tuples and generators.
> print x
> y = math.sqrt(x)
> print y
> #area = math.sqrt((s)(s - a)(s -b)(s-c)) --this is the formula that
> will not work
> print "This is the area of the traingle: " + area
>
> main()
>
> # This algorithm has type conversion problems.
> # Error message: 'float' object is not callable
FWIW the simplest example of the callable issue is:
>>> 2(3)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: 'int' object is not callable
>>> 2*3
6
>
>
--
Bob Gailer
510-978-4454
More information about the Tutor
mailing list