My professor wants me to create a function that returns the sum of the numbers in a string, but without using any lists or list methods.
The function should look like this:
>>> sum_numbers('34 3 542 11')
590
Typically, such a function is easy to create using lists and list methods. But trying to do it without using them is a nightmare.
I tried the following code, but they do not work:
>>> def sum_numbers(s):
for i in range(len(s)):
int(i)
total = s[i] + s[i]
return total
>>> sum_numbers('1 2 3')
'11'
Instead of getting 1, 2, and 3 all converted to integers and added together, instead I get the string "11". In other words, the numbers in the string still have not been converted to integers.
I also tried using the function map(), but I got only the same results:
>>> def sum_numbers(s):
for i in range(len(s)):
map(int, s[i])
total = s[i] + s[i]
return total
>>> sum_numbers('1 2 3')
'11'