Python :TypeError: unsupported operand type(s) for +: ‘int‘ and ‘str‘问题解决办法

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值