I'm not actually stuck on this exercise, I am just wondering if there is an easier way to complete it than what I did. It took quite a bit of debugging to get it to function properly. If anyone has any tips on how it might have been simplified, please let me know :slight_smile:

def digit_sum(n):
    total = 0
    number_of_numbers = 0
    n = str(n)
    while number_of_numbers < len(n):
        for number in n:
            number_of_numbers += 1
            total += int(number)
    return total


you could just use a for loop to loop over n?

n = str(n)
for i in n:
   # add to total

this safes quit a few lines of code


Thank you! It's really interesting going through this stuff without much guidance, but I am enjoying having to think through it instead of having the steps laid out for me. :slight_smile:


yes, it is great for improving your programming skills

that is good, since without the steps you are programming, you have to think how to design the program and what you need and so on.

if you have the steps you are more coding then programming (yes, programming and coding are different)


whats wrong in this piece of code

def digit_sum(n):
2.     sum=0 
3.     while(n!=0):
5.         digit=n%10
6.         n=n/10
7.         sum=sum+digit
9.     print sum



you should return the sum, you print it, but don't return it

also, consider not to use sum since it is a built in function name


Ok Sir I'll keep it in mind next time.