4. digit_sum -- what is wrong with my code?


#1


What is wrong with my code? It says my function fails at digit_sum(434) it returns 4 when it should be 11.


Replace this line with your code. 

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


#2

a function ends the moment a return keyword is reached

if the return keyword is reached in a loop, the loop will break in order for the function to end

so your loop breaks in its first iteration, is this what you want?


#3

So my whitespace was the problem. getting rid of an indent at return fixed it, thanks!


#4

I'm curious as to why it gave me that specific error rather than something else? Was that an arbitrary number that code academy tested in my code, and any number entered would have returned the first digit due to the loop ending prematurely due to my extra indent?


#5

codecademy has a bunch of test cases, the first one that failed was 434, each test case has an error message. If you are exactly told what to do, you will never learn to debug or program, then you will only learn how to code


#7

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