4.Digit_sum..Please help


my code is:
also it is intended properly

s = 0
def digit_sum(n):
    while n != 0:
        remainder = n % 10
        s = s + remainder
        n = n / 10
    return s   

print digit_sum(123)

The error is :-

Traceback (most recent call last):
File "python", line 10, in
File "python", line 5, in digit_sum
UnboundLocalError: local variable 's' referenced before assignment

I am not able to move forward. Can somebody help..


Assign s = 0, inside of your function.


i have tried that also,but it throws an error
Oops, try again. Did you create a function called digit_sum? Your code threw a "local variable 's' referenced before assignment" erro


Post the code with that included, it should work with that.


Plz post full code or we cant help:grinning:


Try this

def digit_sum(n):
while n != 0:
s = 0
remainder = n % 10
s = s + remainder
n = n / 10
return s

print digit_sum(123)


s should be defined outside of the loop, else it gets reset to zero on every iteration.


Sorry, I pasted it in the wrong place:slight_smile:


That's where typing code has the advantage. We type as we think, and the code flows out naturally, especially once our fingers get trained.


You type as you think. I'm a terrible typer :sweat_smile:


Everybody is terrbile at some point (when starting out) but it only gets better, especially with practice. Typing is a fundamental skill. Dig around and look for a typing tutorial that teaches rest position and touch-typing where you never take your eyes off the screen. Work toward a speed of better than 30 words a minute with accuracy in the high 90 percentile. It won't come quickly but it is doable.


I can type quickly and accurately with words, but it's a lot harder when the coding elements are thrown in.




Again, this improves with practice. Pick a line of code and type it over and over and over and over, again. Don't stop to fix mistakes, just make less as you go. When you can comfortably type the line with no mistakes, start with another one.

    if (choice1 === "rock") {
       if (choice2 === "paper") {


Thank you, I'm sure that will help.


ありがと! Are you a moderator?


Yes, but that only means we are forum caretakers. We have a broom and dustpan issued on our first day.


:joy: That's amazing. My friend is trying to become a moderator.


It won't happen instantly, or even quickly. On average, forum moderators here have been active contributing members for over two years. We do keep a 'watch list' of potential candidates but it can be a long time between appearing in the list and actually being approached/invited to the team. It is very important for maintaining continuity and unity as a team.


I am sorry but none of your solutions work.. :sweat: