Code academy bug or my code is wrong?


Codeacademy bug?

Codeacademy is saying that the function is failing in digit_sum(434) is returning 22 not 11

But in my screen the function is returning the right answers (digit_sum(434) returns 11)

answer = 0

def digit_sum(n):
    n = str(n)
    for i in range(len(n)):
        global answer
        answer = answer + int(n[i])
    return answer
print digit_sum(1000)


Woah! I just tried it out and true that.


>>> digit_sum(11)
>>> digit_sum(11)
>>> digit_sum(11)
>>> digit_sum(11)
>>> digit_sum(11)
>>> digit_sum(11)


Sorry, I don't seem to understand why this. :slight_smile:


Hi @bayoishola20 ,

Given the definition of the digit_sum function in the original post, and calling it multiple times ...

  • What should the value of answer be each time the digit_sum function begins to execute?

  • What is the actual value of answer each time the digit_sum function begins to execute?


Woah! That's true. I didn't consider the multiple scenario. I guess @aliibrahim23 's issue though, is that the console was pointing at something that was right and calling it wrong which you've now clarified.

@aliibrahim23 , please, consider what @appylpye is pointing at. :slight_smile:


So, then, what is the source of the problem, and what is the remedy for it?


global is mostly used to change or create global variables in a local context. So, the global statement for the variable answer ,becomes available "outside" the scope of the function, effectively becoming a global variable.

A more suitable way to effectively write the code for the exercise, will be:

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


Thank you for your help but can you explain what was the mistake in using the global error? Thanks :slight_smile:


Hi, @aliibrahim23 ,

If answer is global, and you call the digit_sum function multiple times in the same program, what is the value of answer each time the digit_sum function begins to execute?

If you are not sure, try printing the value of answer from within the function. Then, explain the result.


So global stores its value inside the function ignoring the statements outside it?


Hi @aliibrahim23 ,

In your original code, you had this outside the function definition ...

answer = 0

Therefore, no matter how many times you call the digit_sum function within your program, that line only executes once. That is a problem, because you need to have answer initialized to a value of 0 each time you begin to add up the values of the digits in a number. If you place the initialization mechanism inside the function, then it can execute each time the function is called.

Whenever possible, design functions to be self-contained units. Occasionally, there is a good reason to have a function use a global variable. Only use global variables when you have a good reason to do so, otherwise you are introducing unnecessary complexity to your program, and increasing the likelihood of bugs.


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