我有一个Python程序,可以将字符串与整数相加:
a = raw_input("Enter a: ")
b = raw_input("Enter b: ")
print "a + b as strings: " + a + b
a = int(a)
b = int(b)
c = a + b
str(c)
print "a + b as integers: " + c
我遇到了这个错误:
TypeError: cannot concatenate 'str' and 'int' objects
我如何将字符串添加到整数?
str(c)
(尽管你所说的必须分配函数结果是正确的)。 - Levonprint "a + b as integers: ", c
。 - Tim Pietzcker