Need help on 4/15


#1

This is what I write for 4/15:

def digit_sum(n):
word = 'str(n)'
for x in word:
num += int(word[x])
return num

It keeps on telling me "Your code threw a "local variable 'num' referenced before assignment" error.
What's wrong with my code/logic? Thanks a lot


#2

That's because on this line you wrote

num += int(word[x]) #same as num = num + int(word[x])

Meaning you are trying to add something to the existing value of num but you have not defined num anywhere

and you should correct your code to this

num += int(x)

but first you must set

num = 0

before the for loop