Digit_sum


#1


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


i did what gathered to be the right answer but it returns that i have an indentation error

I don't fully understand how to do this problem without help of the forum i feel it should be correct but could be missing a simple part


def digit_sum(n):
   n = str(num)
   l = len(n)
   number = 0
   while l < 0:
       for digit in n:
           number += digit
        return number


#3

Actually I don't understand your two loops ...

You should only have one. Plus the while loop is not incremented (l-=1)

Another thing, you have to check if it is possible to sum up the digit, because they are characters (since you converted num to string.

I think that you should change your code by removing the while loop and adding a convert to integer for digit before summing.

def digit_sum(n):
   n = str(num)
   l = len(n)
   number = 0
       for digit in n:
           number += int(digit)
   return number

#4

change n = str(num) to n = str(n) and it works. Thank you for doing the hard part because I was way off.


#5

Ahahah missed that one ...

Thanks ajax :smiley:


#7

Here is another solution:

def digit_sum(n):
    n = str(n)
    summ = 0
    for i in n:
        summ += int(i)
    return summ

#8

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