4. digit_sum



Hi everyone, could you help solve my problem?
I think my idea is right, but it dose not work.

def digit_sum(n):
    while (n>=10):
    for item in range(i+1):
    return sum


Should you really be using two loops? If you do it manually, to you read each digit twice? Or is it enough with one pass? And if once is enough, then what does the other loop do? What exactly should be done for each digit? Is either loop doing that something?


n= raw_input ("chose a number with four digits :")

def digit_sum(n):
x = str(n) #change to string
num = 0
for digit in x:
num=num + int(digit) #change to integer
return num
print digit_sum(55)


I understand your way is more simple.
And I also know my way is inefficient, but could you help me figure out why it does not work?