How can I break down these problems into easier parts?

Good point. That is why after writing the code you can refactor. Refactoring can also be to make the code more readable

but just wanted to show you the shorthands as well

This works for positive and negative integers using abs(). However, as the challenge assumes the integer is positive, there’s no need for the abs().

def digit_sum(n):
a = 0
for x in str(abs(n)):
a += int(x)
return a

def digit_sum(n):
n = str(n)
total = 0
for i in range(len(n)):
total = total + int(n[i])

i = i + 1

print (n)

Your function fails on digit_sum(434). It returns None when it should return 11.

Can someone explains what is this error about? I’ve tried to solve my problem in different ways, even trying with just n as a variable but I can’t get a solution.

I believe the trick is in adding strings, though could someone show me a light?


What do they say about a function that has no explicit return value?

1 Like

So you’re saying I should put a return to retreive some value after the for block?

because if so, I changed the code adding the return statement

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

It still gives this error:

Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a “‘int’ object has no attribute ‘ getitem ‘“ error.

What does it means?

It traces back to the for loop iteration variable’s value. i in the above is not an index, but the actual value in the current element. Since we are already iterating n we need only cast the value to an int.

total = total + int(i)

Be sure that the return statement is not inside the loop block. Recall that anything after return is unreachable, so the print statement is superfluous if it is inside the code block. If it is outside, the total variable is not accessible.

print (digit_sum(1234))    # 10

As for the no attribute **getitem**, that says an integer cannot be treated as an iterable (with an index). Iterables would be expected to have a getitem attribute so that polling by index is supported.

Moot, now that we know it is values at play, above, and not indices.


thanks for this. simplest way i could thing off.

def digit_sum(n):
x = 0
for i in str(n):
x += int(i)
return x
print digit_sum(456)

= 15

While trying to compute a factorial… I was trying a method very diff. from the solution provided… (as I was not able to do it I had to look at the solution)
could you please take a look at it and tell me what I am doing wrong and if I could have achieved the output this way?

To start off I war trying to create a list which included all the numbers from the input number to 1

Then I was trying to multiply all the items in the list with each other one after another.


There’s no question in this comment !
Hi again!

I had to complete it my way and it’s finally done.
After having understood your solution properly I do not see the point in doing it through the list like how I have. but for some reason that’s the only way I thought of doing it and wanted to complete it.


1 Like