Python :TypeError: unsupported operand type(s) for +: ‘int’ and 'str’问题解决办法
运行以下程序
X = R * np.sin(theta) * np.cos(phi)
Y = R * np.sin(theta) * np.sin(phi)
Z = R * np.cos(theta)
d=1
for i in X:
for j in Y:
for k in Z:
f = open('C:/Users/LAB103-06/Desktop/xyz.txt','a')
print(d+" "+str(i)+" "+str(j)+" "+str(k)+" "+'1'+" "+'1'+" "+'1'+'\n')
f.write(d+" "+str(i)+" "+str(j)+" "+str(k)+" "+'1'+" "+'1'+" "+'1'+'\n')
d=d+1
f.close()
报错:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
因为“+”前后连接的数据类型不同,一个是整型,一个是字符串类型
解决办法:转换成相同类型
X = R * np.sin(theta) * np.cos(phi)
Y = R * np.sin(theta) * np.sin(phi)
Z = R * np.cos(theta)
d=1
for i in X:
for j in Y:
for k in Z:
f = open('C:/Users/LAB103-06/Desktop/xyz.txt','a')
print(str(d)+" "+str(i)+" "+str(j)+" "+str(k)+" "+'1'+" "+'1'+" "+'1'+'\n')
f.write(str(d)+" "+str(i)+" "+str(j)+" "+str(k)+" "+'1'+" "+'1'+" "+'1'+'\n')
d=d+1
f.close()
问题解决:
106723 2.528673749190052 -60.21496407783437 -26.73478015524438 1 1 1
106724 2.528673749190052 -60.21496407783437 -27.866101667264314 1 1 1
106725 2.528673749190052 -60.21496407783437 -28.99464568556241 1 1 1
106726 2.528673749190052 -60.21496407783437 -30.121781865381834 1 1 1
106727 2.528673749190052 -60.21496407783437 -31.24957082386987 1 1 1
106728 2.528673749190052 -60.21496407783437 -32.381249956266075 1 1 1
106729 2.528673749190052 -60.21496407783437 -33.52217836770869 1 1 1