Digit_sum


#1

Help I am stuck and it keeps telling me:

function has no positive integer and "int objetc is not iterable"


#2

the url is the same for everyone, it checks for user to load your personal code

Please copy paste your code to the forum, preferable using markup:

So the indent will show


#3

n=int(input('input a number'))
def digit_sum(n):
    result=0
    n=int(str(n))
    for i in n:
        i +=int(i)
    return (sum(result))


#4

this line:

n=int(str(n))

you cast n to a string to cast it back to a integer. Could you explain why?

here:

i +=int(i)

you are adding int(i) to the for loop iterator, why? You defined a beautiful variable called total, which i assume has as purpose to store the total?

don't use sum(), it is for lists. You don't have a list.


#5

n=int(str(n)) because the hint reads "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."

I dont have a variable called total. I only have a variable called result so you have confused me

Anyway please help me correct it because I seem not to understand your feedback. please forgive for that


#6

you do the sub-string conversion in your for loop:

i +=int(i)

to get the result.

but here:

n=int(str(n))

you are converting the whole string back to an integer after casting it to a string, not the sub-string.

That is indeed a mistake of me, that should be result:
`
You defined a beautiful variable called result, which i assume has as purpose to store the result?


#7

am stuck again
help


#8

you still add i to i instead of result, result should have the sum of the numbers. adding to the iterator doesn't make much sense since its get overwritten every iteration of the loop


#9

my mistake
got it now
i changed to
result =result+i

Thanks alooooot
much appreciated


#10