简体   繁体   中英

Why am I getting error. TypeError: unsupported operand type(s) for +: 'int' and 'str'?

I am currently having trouble with a program in python3.

I have recently come across the error: TypeError: unsupported operand type(s) for +: 'int' and 'str'

I have no idea what the problem could be so any help would be much appreciated.

lengths = input("Enter the Lengths of the Sides of the Shape Seperated by commas: ").split(',')
            answer = sum(lengths)

            print("+".join(lengths) + "= %s" % (answer))

"lengths" is a list containing strings. "sum" operates on numeric types. You need to convert strings to floats. Try to add this line before sum, and sum on lengths_float:

lengths_float=[float(element) for element in lengths]

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