Code academy bug or my code is wrong?


#1



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)


#2

Woah! I just tried it out and true that.


#3

>>> digit_sum(11)
2
>>> digit_sum(11)
4
>>> digit_sum(11)
6
>>> digit_sum(11)
8
>>> digit_sum(11)
10
>>> digit_sum(11)
12

#4

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


#5

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?


#6

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:


#7

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


#8

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
        
digit_sum(434)

#9

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


#10

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.


#11

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


#12

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.


#13

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