TypeError: Unsupported Operand Type(s) For +: 'int' And 'str' When Using Str(sum(list))
I'm going through the Python tutorial and have no idea why my code isn't working. I know I need to tell Python to print an integer manually but I have already put str(sum(ages)). C
Solution 1:
The issue is that you can't use sum
on a list of strings. In order to do this, you can use a generator expression to convert each element to an integer first:
print('The combined age of all in the list is ' + str(sum(int(x) for x in ages)) + '.')
Which gives us:
The combined age of all in the list is 251.
Post a Comment for "TypeError: Unsupported Operand Type(s) For +: 'int' And 'str' When Using Str(sum(list))"