4. digit_sum: adding items in the list


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


When I run this code, an error pops up. The error message reads, "Oops, try again. Your function fails on digit_sum(434). It returns 21 when it should return 11."


However, I believe my function should return 11. I'm not sure if something's wrong with the function or the end part when I did sum(list).


list1 = []

def digit_sum(n):
    while n != 0:
        list1.append(n % 10)
        print list1
        n = n // 10
    return sum(list1)


digit_sum(1234)

I have no idea how I'm getting this wrong. I've seen other codes in different discussions on this topic, however, I would like to try to solve this problem on my own...
It's just that I got too frustrated to see errors whenever I tried to fix it -
Please help me!! x(


#2

Someone had discussed the same problem that you had, last week.

Refer to this link

I hope that's enough for you to get the idea on what to fix.


#3

def digit_sum(n):
i = len(str(n)) - 1
s = n // (10 ** i)
while i > 0:
s = s + n % 10
n = n // 10
i -= 1
return s


#4

I'm not quite sure what this is about:

or this:

Could you explain it?


#5

Hi ,
i think the problem is that list1=[] is before fucntion

your code works for 1 test but if 2 list will add both
it works 1st time it store 1,2,3,4 if next test has 434 list will be [1,2,3,4,4,3,4]
so u may put the list in start of function

def digit_sum(n):
	list1=[]
	while n != 0:
		list1.append(n % 10)
		print list1
		n = n // 10
	return sum(list1)

or may try emty the list after sum in return don't know if it works or no
this what happen when testing ur code 3 successful times
`

def digit_sum(n):
while n != 0:
list1.append(n % 10)
print list1
n = n // 10
return sum(list1)

digit_sum(123)
[3]
[3, 2]
[3, 2, 1]
6
digit_sum(123)
[3, 2, 1, 3]
[3, 2, 1, 3, 2]
[3, 2, 1, 3, 2, 1]
12
digit_sum(123)
[3, 2, 1, 3, 2, 1, 3]
[3, 2, 1, 3, 2, 1, 3, 2]
[3, 2, 1, 3, 2, 1, 3, 2, 1]
18
`


#6

There's easiest way:
def digit_sum(n):
amount = 0
text = str(n)
for i in text:
amount = amount + int(i)
return amount


#7

i think your code is correct you should add a print command before your digit_sum() and then enter 434 it will return 11 only not 22


#8

the list1.append(n%10) actually adds the remainder after dividing n by 10 to the list
and the n=n//10 divides n by 10 and rounds it off to the left of the number line


#9

I did using the next code:

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


#10

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


#11

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

The above works as well using modulus and floor


#12

Thank you.
After I've put the list1 = [] in my function, it worked perfectly! :smiley:


#13

Thank you everyone for leaving an advise for me.
Sorry that I couldn't take a look at them until now.
I've solved a problem, and I really appreciated all the answers.
Again, Thanks!


#14

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