Digit_sum, i've been trying for hours. Plz help


#1




as you can see from the image, the code produces the right result.
If i put the same line of code under a "def" it wont work. Why?

Why do I have to translate the n to str?
why do i have to translate after to int?
Why not directly to int?
And whats the deal with positive int?


n = 863
n = str(n)
total = 0

for i in n:
    total += int(i)

print total


#2

can i see your code which does contain a function? Because you need the function to pass

because you can't loop/iterate over integers??

because if you don't you just join two strings together:

print "2" + "2" # 22
print 2 + 2 # 4

i don't understand this one, what do you mean?


#3

> def digit_sum(n):
>     for i in n:
>         total += int(i)

so this means that when integer as the word suggests it is interpreted as 1 whole. Not seperate pieces?

right, makes sense.

this is from the error code I'm getting:

"Oops, try again. Does your digit_sum function take exactly one argument (a positive integer)?"

Thanks for your help


#4

i don't know in detail how integers are implemented (not from the top of my head), and why you can't iterate over them, but if you want i can search this for you.

in your for loop, we determined that n is a integer, what did we say about integers and looping?


#5

I'll research this, if you have a specific link on where I can find more info it would be appreciated.

But it works perfectly without the def... That's what I dont understand. Look at the first image I posted. It gives the correct answer.


#6

let me see what i can find it

because you convert n to a string? so you need to do this in the function as well.

Can i see your function code? I think that is easier

i can't find much, only that a TypeError is raised, which means you use a function (in this case for) for a datatype (integer) which doesn't support this


#7

this results in "0" and error code. In the one below, I just remove DEF, all the rest stays the same.

n = 86309
n = str(n)
total = 0

def digit_sum(n):
    for i in n:
        total += int(i)    
    
print total

n = 86309
n = str(n)
total = 0

for i in n:
    total += int(i)    
    
print total

The only difference is that this code produces the right result. But it has no def.

How is the def impacting the result i dont understand. The loop stays the same.


#8

the exercise is going to call your function multiply times with different integers to test it works correctly.

this means, this two lines:

n = str(n)
total = 0

should be inside the function. The validation of the exercise only executes the function, not the whole script.

It is vaguely in the instructions, your function needs to be capable of returning the sum. Not additional things inside the script should be needed to achieve this goal


#9

got it.

> def digit_sum(n):
>     n = str(n)
>     total = 0
>     for i in n:
>         total += int(i)
>     return total

this was tough. Thanks for your help. I'm going to bed, let me know if you find this link.

Thank you so much


#10

I found this one was solved better using recursion. You can iterate over the digits of a number by returning:
x %10 + digit_sum(x / 10) and return 0 when x is <= 0. This will keep adding the last digit every time it performs the mod 10 and you drive your number down to zero by dividing by 10 every time.


#11

you can also do this approach with a loop instead of recursion


#13

The def() is not working because before you print total you have to first call digit_sum(). If you don't, there will be no such variable as total, and Python will give you an error message!


#14