def digit_sum(n):
    for number in n:
        num = str(number)
        for numerical in num:
            nums = int(numerical)
            final = sum(nums)
    print final

Not understanding what to code for this level. Still somehow managed to write this. Help needed.


for number in n:

n is a integer, integers are not iterable. that is why you convert to a string (strings are iterable), and loop over the string, which is what you do here:

num = str(number)
for numerical in num:

sum() is a built in function to get the sum of a list of integers, which is not what we have.

so, instead before the loop declare a variable final and set it to zero

Then in the loop, simply add nums to final using +


Still not understanding brother. Can you write that piece of code again with changes??


Then tell me which bit of your explanations you do not understand

My answers are designed so you still have to think it through, so you learn more then just being provided with the answer


After iterating through the strings how would i add them up if not using the sum() function?


well, python has basic math operators:

5 + 5

we can simply use + to increase a number. Make sure to update the variable.

If you need more help, can you also include an updated version of your code?


def digit_sum(n): (Defining the function with integer)
nums = str(n) (Converting the number into a string)
for number in nums: (Iterating through the string)
numerical = int(number) (Converting the string back to number)

Now what? Confused!


good, this is already better.

outside our loop, we can create a variable called total:

total = 0 # for storing the total of n
for number in nums:

now that we have this, we can add to total:

numerical = int(number)
total = ??

how can we add numerical to total? we have to use the to the plus sign (+), how do you think we can do this?

if you feel more comfortable with sum() you can of course make an empty list, append the numbers to the list, and then use sum()


def digit_sum(n):
    num = str(n)
    total = 0
    for number in num:
        numerical = int(number)
        total  = numerical + ?
        return total


the question mark, what do you it should be? We need to add numerical to what is already stored in total, here is little hint:

example = 0
example = 5 + example

that should help

Finally, a function ends the moment a return keyword is reached. If this happens in a loop, the loop breaks

So place the return outside of the loop


def digit_sum(n):
    num = str(n)
    total = 0
    for number in num:
        numerical = int(number)
        total  = numerical + total
    return total

Got it brother i was placing the return total within that loop. Now understood. Thanks a ton.


if you like a challenge, try your sum() approach.

instead of total = 0 make an empty list.

in the loop, .append() numerical to the list

after the loop, use sum() on the list


Why wouldn't this work?

nums += int(str(len(n)))


if we have 434, the length is 3, which we then cast to a string, then to a integer. So in the end, we have 3.


numerical = int(number)
total = numerical + total

is the same as:

total += int(number)


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