首页 > 解决方案 > 不断收到类型错误:+ 的不支持的操作数类型:python 中的“int”和“str”

问题描述

开始上 Python 课程,并被要求调试一个小程序。除了这一部分之外都完成了

a = 2             # Declare a variable with a value, 2
b = 19             # Declare a variable with a value, 19               
c = a + (".") + b       # Concatanate strings, a, ".", and b 
print (type(c))     # print c's type
print (c)           # print c

预期产出

class<str>
2.19

无法计算出这个小数点,我知道它与此有关。

标签: pythondebugging

解决方案


a并且b是 int 类型。您必须将它们更改为字符串才能进行连接:

a = 2             
b = 19             
c = str(a) + "." + str(b)       
print (type(c))    
print (c)      

推荐阅读