4. digit_sum Error unsupported operand types after I converted


Thanks in advance for any assistance. I have looked at other examples (not copied and pasted m8s), yet I still cannot get this to succeed.


Oops, try again.
Your function fails on digit_sum(434). It returns None when it should return 11.

new_list = [] #making an empty array
new_list2 = []
def digit_sum(n): #def a function
    string = str(n) #converting parameter to a string to split it
    for x in range(0, len(string)): #looping through each index and splitting it
        new_list.append(string[x]) #adding the split string to a new list called new_list
    for x in range(len(new_list)): #looping through each index and making it a integer #doing that ^
    print sum(new_list2) #adding all the values together


The above lines may be removed. Your new_list contains the digits.

Use return as the error message implies.

return sum(new_list)


So the error was using print instead of return?



Write a function called digit_sum that takes a positive integer n as input and returns the sum of all that number's digits.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.