我想获得abs
float
str
,for q in range(0, len(moms), 1):
print("mom types", type(moms[q]))
out.write(abs(float(moms[q]))+" ")
out.write("\n")
最初存储在mom types <class 'str'>
Traceback (most recent call last):
File "s2gen.py", line 192, in <module>
out.write(abs(float(moms[q]))+" ")
TypeError: unsupported operand type(s) for +: 'float' and 'str'
中:
{{1}}
给出错误:
{{1}}
答案 0 :(得分:3)
您无法添加float
值和str
值;在为其添加空格之前,您必须明确地将abs
函数的结果转换回str
。
out.write(str(abs(float(moms[q]))) + " ")