4. digit_sum


#1

def digit_sum(n):
x=n%10
n=n//10
y=n%10
n=n//10
z=n%10
return x+y+z
digit_sum(434)

This is the code I wrote using the modulo method.. but it doesn't take into account numbers greater than 999.
How to make it a generalized code?


4. digit_sum Modulo Help
#2

You should be using a for-loop to repeat the process for the number of digits in the number.
Then you would want to obtain the sum by adding it to a variable for that same number of times.

Hope this helps!


#3

I am not against using for-loop. But in my opinion, for this example, using while loop will be way easier to code and to read for everyone else.
Hints:

  1. initialize a variable to hold the sum before while loop.
  2. Think of the number (very popular number, remember i'm saying "the number") when you would want while loop to stop.
    #Inside while loop
    -number modulus by 10
    -sum adds to itself and the digit
    -divide the original number by 10 and store it to itself. (This part could be little trickier)
  3. Return sum

Hope this helps :slight_smile:


#4

Hi,

I'm attempting the same question (sorry if piggy-backing isn't allowed, it's my first post!)

I got to this point, but it's not working. Argh! It seems to only go through the while loop once instead of as many times as the length of the number. Ie for 434 it returns 4 instead of 11, for 123 it returns 3 instead of 6 etc.
Any advice?


#5

never mind - got it - just had the indentation wrong for my 'return'


#6

def digit_sum(n):
result = 0
while n >= 10:
lastDigit = n % 10
print "The current last digit is " + str(lastDigit)
result += lastDigit
n = n // 10
print "The next number will be " + str(n)
else:
result += n
return result

print digit_sum(45367343241)

works on any positive integer


#7

This works for me with a for-loop

def digit_sum(n):
    result = 0
    m = str(n)
    for i in range(len(m)):
        lastDigit = n % 10
        print 'Current last digit: ' + str(lastDigit)
        result += lastDigit
        n = n // 10
    return result

print digit_sum(123456)

#8

Here's mine, hope this helps someone.

def digit_sum(n):
    num_list = [] #make empty list
    for number in str(n): #turn it into string
        number = int(number) #then back into number
        num_list.append(number) #add all of them to the empty list
    n = sum(num_list) #sum up all numbers in list
    return n #return the sum

digit_sum(434) #use any number

#9

Here's how I did it - short and does the job, and has a very readable print out:

def digit_sum(n):
    total = 0
    print "The Math:"
    print
    for digit in str(n):
        print "%s + %s = %s" % (total, digit, total + int(digit))
        total += int(digit)
    print "__________"
    print "Total = %s" % total
    return total
    
digit_sum(1337)

What this prints out:

The Math:

0 + 1 = 1
1 + 3 = 4
4 + 3 = 7
7 + 7 = 14


Total = 14

What it looks like with no printing:

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

#10

i did the same what you did and it worked but i edited some variables now please explain it for me because i can't understand it


#11

m = str(n)  # convert the number to string, so "for loop" can iterate 
for i in range(len(m))  # for loop iterates till the end of the previously converted number
lastDigit = n % 10  # if you use modulo on a number it gives you the last number. e.g: 1234 % 10 = 4
result += lastDigit  # add the last number to the result
n = n // 10  # cut the last number. e.g: 1234 // 10 = 123

I hope this is clear :slight_smile:


#12

Hi folks,

This is my paltry attempt; it seems to get stuck on the first number. Can someone explain why it's not working?

def digit_sum(n):
total = 0
control = len(str(n))
print control
for number in str(n):
if control > 0:
total += int(number)
print total
control -= 1
print control
else:
print total
return total

digit_sum(7234)

Cheerios


#13

nevermind, return was indented in the for loop, when it needed to be back one. small fix :slight_smile:


#14

Hello guys,
It looks like my code meets the requirements, the codeacademy does not think so though. What is wrong? It keeps saying Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11. Although I can obtain 11 with my written code.
Here is my code:
def digit_sum(n):
summa=0
for l in str(n):
summa=summa+int(l)

print summa

digit_sum(434)


#15

Sorry, found the problem. I have forgotten to return the output


#19

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


#20

def digit_sum(n):
    number_list = []
    for c in str(n):
        number_list.append(int(c))
    return sum(number_list)
        
print digit_sum(434)

#21

this makes the most sense for me. Thank you!


#22

def digit_sum(n):
ds = str(abs(n))
s = 0
for i in range (0, len(ds)):
s += int(ds[i])
return s


#23

thanks for posting this is almost exactly how I wrote mine. can you remind me why the i is in brackets?