首页 > 编程语言 > 详细

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

时间:2021-04-07 12:45:59      阅读:29      评论:0      收藏:0      [点我收藏+]
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))

技术分享图片

 

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

原文:https://www.cnblogs.com/tangchunlitester/p/14626360.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!