# Im confused...4.digit_sum

#1

def digit_sum(n):
__for a in str(n):
____aa = int(a)
____print aa[2]

print digit_sum(123)
prints:
1
2
3
but :
def digit_sum(n):
__for a in str(n):
____return int(a)
print digit_sum(123)
prints:
1
Whaat?
BTW is this code even close to the answer?

#2

def digit_sum(n):
x = str(n)
y = len(x)
s = 0
for z in range(y):
s = s + int(x[z])
return s

#3

The return will give you one value and end the function at that point, that's why it stops at that point in the first run.

Your program has to take an integer as input, like 12, 344, 654 for example.

Then it sums each number: 123 = 1 + 2 + 3 = 6

That's what your program has to do.

So try to add each number into a new variable and return it when the for-loop is done.

#4

You have the right idea, just a little bit off. You are going to be passed a number but you want to add each number individually, so you will convert the number to a string ( str(n) ) in order to access each piece. You then need some kind of running total, so instead of

```aa = int(a)
try
aa += int(a).
This is the same as:
aa = aa + int(a)```

You need to return this running total now though once everything is added, so put a return statement outside of the for loop where your running total is, but still in the function. You may also need to set aa=0 in the beginning of the function, just so aa will be known to be a number and can be added to.

#5

Thank you so much!You saved my life!
def digit_sum(n):
____Thanks = 0
__for a in str(n):
____Thanks += int(a)
__return Thanks
print digit_sum(123)

#6

I understand all of your code except for one part.

s = s + int(x[z])

What is happening in the int(x[z]) section??