简体   繁体   中英

keep getting typeerror: unsupported operand type(s) for +: 'int' and 'str' in python

Taking a beginning Python class and was asked to debug a small program. Have all of it done except this one part

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

Expected output

class<str>
2.19

can't figure out this decimal point I know it has something to do with that.

a and b are of type int. You have to change them to strings to do the concatenation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM