Python学习问题记录-报错 TypeError: can only concatenate str (not "int") to str

print('((993+196) * 7) / 3的商为 ' + ((993+196) * 7) // 3)
print('((993+196) * 7) / 3的余数为' + ((993+196) * 7) % 3)

运行代码报错:

Traceback (most recent call last):
File "D:/testing_study/python/python_learn/py_001.py", line 1, in <module>
print('((993+196) * 7) / 3的商为 ' + ((993+196) * 7) // 3)
TypeError: can only concatenate str (not "int") to str

 

这个错误的意思是类型错误:字符串只能拼接字符串。

 

解决的办法
通过str()函数来将其他类型变量转成String。

正确代码如下:

print('((993+196) * 7) / 3的商为 ' + str(((993+196) * 7) // 3))
print('((993+196) * 7) / 3的余数为' + str(((993+196) * 7) % 3))

 

posted @ 2021-04-07 09:42  糖糖来迟  阅读(1747)  评论(0)    收藏  举报