Reputation: 13
I am getting this error "TypeError: 'str' object is not callable" when i am trying to convert a str to float inorder to do summation.
# To Find sum of no. s seperated by commas in a string
t=''
y=''
z=""
s = "1.23,2.4,3.123"
for x in s:
if x != ',':
y += x
else:
t = z
z = y
y = ''
print("y=",y,"z=",z,"t=",t)
y = float(y)
z = float(z)
t = float(t)
print("Sum is =",y+z+t)
Screenshot of the error occurring when i run the code
Upvotes: 1
Views: 1031
Reputation: 22564
I see that you ran that code in Spyder. That is a great environment, which I use, but it does have one disadvantage. Any variables you have defined are still defined at the start of your code.
Almost certainly you have defined a variable with the name float
that holds a string value. So your program tries to execute y = float(y)
but misunderstands the meaning of float
. We could have seen that if you had your "variable explorer` window visible, rather than the "help" window in your screenshot.
Try your program again after closing and restarting Spyder and see if you still get that error. And from now on, avoid using standard names like float
for your variables.
Upvotes: 5