Digit_sum


#1

I don’t understand how to divide up a string into separate numbers like it says in the hint. This entire section is really tough

“One way might be to convert the integer to a string with str(), divide it up, and turn the substrings back into integers with int() to do the addition.”


#2

Its designed to be, up to this point, you have been coding. This section is trying to teach you how to program (and no, coding and programming is by a long shot not the same thing)

well, strings are iterable (can be looped over), does that help?


#3

I’m still confused. Sorry


#4

Tried a different tactic:

def digit_sum(n):
x = n
times = (int(len(str(n))) - 1)
final = 0
while times > 0:
x %= 10
final += x
times -= 1
return final

Didn’t work
Oops, try again. Your function fails on digit_sum(434). It returns 8 when it should return 11.

Edit: I think I know what’s wrong here. Trying something different.

Edit: Did it!
def digit_sum(n):
x = n
times = int(len(str(n)))
final = 0
while times > 0:
final += (x % 10)
x /= 10
times -= 1
return final


#7