【发布时间】:2018-12-30 01:50:02
【问题描述】:
当我尝试将 str 转换为浮点数以进行求和时,出现此错误“TypeError: 'str' object is not callable”。
# 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)
运行代码时发生的错误的屏幕截图
【问题讨论】:
-
当我运行它时完美运行。您可能想使用split 方法
-
您在某处为“浮动”分配了一个数字,尽管它不在您显示的代码中。
标签: python string python-3.x floating-point type-conversion