What am I doing wrong to get this error using the sum function in Python:
student_heights =input("Input a list of student heights ").split()
a=sum(student_heights)
print(a)
User input: 12 13 14 15
Error:
Traceback (most recent call last)
File "main.py", line 2, in <module>
TypeError unsupported operand type(s) for +:'int' and 'str'
student_heightsis a list of strings. You can'tsum()them usefully until you convert them to integers. This will work but depends on error-fee input:a=sum(int(h) for h in student_heights).