-1

I'm a bit new to coding and I have a school project where we have to make a code using python, in the code we get an input from the user and need to add it together in a list, this is the code:

x = input("Enter the expenses: ")
y = x.split()
n = sum(y)
print("Total: $"+ n +"")

I have looked all over the internet and apparently this is correct but for some reason I always get an error saying:

Traceback (most recent call last):
  File "program.py", line 3, in <module>
    n = sum(y)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

What am I doing wrong?

jordanm
  • 33,009
  • 7
  • 61
  • 76
Caleb
  • 1
  • 1

1 Answers1

0

The elements of y are strings so you can't sum it up. Change then to ints first like:

y = list(map(int, y))

and then do the sum

user15270287
  • 1,123
  • 4
  • 11