1

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
4
  • Do you define a variable named float or singles somewhere? Commented May 22, 2014 at 14:24
  • @VincentSavard I define singles as shown in the second code block. Commented May 22, 2014 at 14:25
  • move all float inside the definition of "singles" and avoid using the same name for a variable and for a function. Also, try to pass "line" instead of separate elements. This might help Commented May 22, 2014 at 14:26
  • @VincentSavard: singles = singles(...). Commented May 22, 2014 at 14:27

1 Answer 1

7

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

If the singles value is not required further, you can just print('Singles: {}'.format(singles(*map(float, line[5:9]))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.