4. digit_sum Error unsupported operand types after I converted


#1



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.

My_Link

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 ^
        new_list2.append(int(x))
    print sum(new_list2) #adding all the values together


#3

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

Use return as the error message implies.

return sum(new_list)

#4

So the error was using print instead of return?


#6

Yes.

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


#7

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