4. Digit_sum


#1



4.Digit_sum


I just can't figure out how to do this one correctly. I had code earlier that I felt was super close but I started over and am now lost.


it should run through so that whatever number inserted will equal the sum of those numbers:
123 = (1 + 2 +3)

lists = []
def digit_sum (n):
for numbers in n:
string = str(numbers)
lists.append(numbers)
for number in range(0,len(lists)):
return int(lists)

`
that is my code so far. any help would be appreciated


#2

You can complete this problem using modulo and floor dividing:

def digit_sum(x):
    s = 0
    while x > 0:
        s += (x%10)
        x //= 10
    return s

However, you are on the right track with your technique. One issue is on this line:

for numbers in n:

Here, you have not converted n to a string type yet. Therefore the program cannot loop through the number. I would suggest the following:

def digit_sum(n):
    result = 0
    for n in str(n):
        #code
    return result

Keep in mind that result needs to be of type int when you return it or add digits to it.

Good luck!


#3

@hawk0920 First of all, try avoiding the word list when coding as it is reserved in Python for specific uses, and that can mess up your code. If you don't want to use floor dividing and modulo to solve it then try this code:

def digit_sum(n):
    word = str(n)
    wordn = list(word)
    integers = [int(i) for i in wordn]
    answer = sum(integers)
    return answer

Hope that works for you :grinning:


#4

hi not so complex,you can reference the following code.hope help.

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

#or
def digit_sum(n):
ds = []
x = 0
while n > 0:
x += n % 10
#x += x
n = n // 10
return x


#5

@wowpalvabbit Love your explanation for this problem!


#6

I just felt like trying this with a recursive function :eyeglasses:

def digit_sum(n):
    if n < 100:
        return n % 10 + n // 10
    else:
        return n % 10 + digit_sum(n // 10)

Simple, elegant and no fooling around with strings.


#7

def digit_sum(n):
sumStr = str(n)
total = 0

for c in sumStr:
    total += int(c)
    print "sum = " + str(total)

digit_sum(434)

The code is indented except when I save the edit in this posting so pls ignore that it doesn't display correctly for some reason. I get the correct output (11) but Codecademy won't let me proceed claiming it doesn't return 11 - just None, which isn't the case. Puzzled....


#8

Hi @adkpaddler,

The problem here is that you are missing a return statement. Although your code does indeed print out the correct output, we need the function to actually return a certain value.

This is fixed by simply placing a statement under your function definition:

return total

That should do it. You can take out the print statement unless you want to keep it there for debugging.

Hope that helps!


#9

Ok the problem here is pretty simple check the code and the explanation is given below:

def digit_sum(n):
temp = n
total = 0
while temp>0:
total = total + (temp % 10)
temp = temp/10
temp = int(temp)
print total
return total

Explanation:
here we initially store the value of n in temp (just as a safety to maintain the original value although not needed) then we define a while loop till the value of temp >0. inside the loop a variable total is used to store the answer total is incremented with the value of (temp%10). in the example in codecademy this would initially return 4 and add it to total. next we divide by 10 and explicitly convert temp to integer. this would leave us with the value of temp as 123. the same process is repeated until temp is greater than zero. Hope this explanation would help


Digit_sum
#10

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