Python报错:can only concatenate str (not "int") to str
案例需求为,接收用户输入的字符并检测其长度,再通过控制台输出
刚开始,百度了一下检测长度的函数为len(str),于是开写。代码如下:
print(" The length is " + len(input("Please input your data")))

发生报错,百度了一下才知道, +号必须要和字符串相连,于是利用str()进行强制转换。
修改后成功运行,代码如下:
print(" The length is " + str(len(input(" Please input your data "))))

浙公网安备 33010602011771号