DIGIT_SUM :it's showing error even though my code is working fine!


#1

su=0
d=[]

def digit_sum(n):
n=str(n)
for x in n:
x=int(x)
d.append(x)
return sum(d)

print digit_sum(1234)


#2

because declaring su and d is not inside the function, calling the function multiply times with different integer values it wouldn't work correctly

Or, if i hire you to write this function, you give me the python script, and i then try to import the function to my script (from yourscript import digit_sum), i get an error because su and d are not defined

the function itself should be fully equipped to calculate the sum of the digits


#3

yep u r ryt i too discovered it now! thanx for ur quick help!


#4