I have a string that is a list of numbers. I need to add them all up and print the total. Everything I try results in a variety of type errors.
number = "12345"
# code???
# result should be 15
The sum() function won't work on the original code, but it does work on a list like this one:
number2 = [1,2,3,4,5]
total = sum(number2)
# result is 20
But then the problem is, I can't figure out how to change the string: number to the list: number2. And honestly, I'm not even convinced that this is what I should be trying to do.
It also works on this variation:
number = "12345"
num = int(number[0]) + int(number[1])
# result is 3
But this is rediculous if the string contains more than a few elements.
I have also tried:
Creating a new list and interating through the string and appending each index value to the new list. (type error)
Casting the string and the string elements
(I have also looked tried to solve this by looking on w3schools docs and stackoverflow posts)
Does the answer maybe have something to do with tuples?