StackExchange User
StackExchange User

Reputation: 1220

Float error not callable

I have a program that makes various calculations based on text provided in a text file. I get an error after the second entry is printed (the first works fine.)

The entry in the file is parsed to be a list:

['Castro, Starlin', 'CHC', '161', '666', '59', '163', '34', '2', '10']

I then make a call to the singles function, which takes four arguments.

singles = singles(float(line[5]),float(line[6]),float(line[7]),float(line[8]))
print "Singles: %s" % singles

The function is as follows:

def singles(a,b,c,d):
    # a = hits b = doubles c = triples d = homeruns
    # hits - (doubles + triples + homeruns)
    tmp1 = b + c + d
    return a - float(tmp1)

This works fine for the first entry:

['Machado, Manny', 'BAL', '156', '667', '88', '189', '51', '3', '14']

and the calculation successfully completes. However, the second is unable to complete:

Traceback (most recent call last):
  File "\\bhmfp\ian.carroll$\Intro to Computer Programming\Project 3\Project 3\main.py", line 107, in <module>
    singles = singles(float(line[5]),float(line[6]),float(line[7]),float(line[8]))
TypeError: 'float' object is not callable

Upvotes: 1

Views: 120

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122023

When you call

singles = singles(float(line[5]),float(line[6]),float(line[7]),float(line[8]))

You replace the function singles with the float returned by that function when it is called. Any subsequent calls to singles will try to call that number, not the function, and thus fail. Use a different name for the return value:

new_single = singles(*map(float, line[5:9]))

(Note use of map, slicing and unpacking to simplify the call.)

Upvotes: 7

Related Questions