DIgit_sum-Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11


#1
def digit_sum(n):
    sum = 0
    while n>0:
        sum=sum+(n%10)
        n=n//10
digit_sum(434)

#2

When i am doing analysis of program it is returning 11 , but i am getting this error message.
What mistake i am doing.


#3

What error message, exactly? And a link to the exercise will be helpful, most certainly. Thank you.


#4

your function fails on digit_sum(434). it returns None when it should return 11
this error message is showing ...


#5

How hard is it to copy the location bar and paste it in?


#6

what location you are talking about...
i am giving you enough information...code and associated error massage...what else ...enter link description here


#7

Bingo! Now we know which exercise. Post your code and we will have something to compare;


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

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


#10

Hey I got the same problem, did you solve it yet?

I tested my code on my own Terminal and it gave the right answer (1) but in the console of Codeacademy it says (12)

> listo = []

> def digit_sum(n):
>     aka = 0
>     n = str(n)
>     for o in n:
>         o = int(o)
>         listo.append(o)
>     for numb in listo:
>         aka += numb
>     return aka

> digit_sum(1000)

Error message

Oops, try again.
Your function fails on digit_sum(1000). It returns 12 when it should return 1.

Pls help me... anyone ^^


#11

A function that does not return anything always returns None

So unless you do now want it to return something I suggest you add a return statement.


#13
def digit_sum(n):
    sum_n = 0
    while n>10:
        sum_n=sum_n+(n%10)
        n=n//10
    return sum_n
digit_sum(434)

I have tried.....its not working.....


#14

We're not getting the highest order digit, which once the sample is reduced is in the first magnitude, less than 10.

    while n > 0:

will reach n in the first magnitude (0 < n < 10).


#15

Totally off topic but related to magnitude in the above discussion.

    10 ** -1 == .10
    10 **  0 ==   1
    10 **  1 ==  10

For what value of n is 10 ** n equal to 0 (zero)? Answer: There is none. Zero is undefined. We can only approach zero from above but never actually reach it in exponential terms. This is the concept of limits in calculus. The closest we can approach zero is 10 ** NegInf. Still not zero, though.

A bogus question, but a rider in the modulo/integer division approach. We have in this solution introduced the concept of shift which is a bitwise term but it applies mathematically to all logarithms in terms of number base. A degree is a degree which is attendant with a shift to the left or the right, depending upon sign, hence, magnitude.

All in good fun, and some chance to learn, as well.


#19

2 posts were split to a new topic: Would you know if this is a possible bug


#26