4. digit_sum


#1



can anyone explain to me what i have to do since i kinda lost in here :confused: i don't want to just copy other people' code so ...
thanks in advance


def digit_sum(n):
    x == str(n)
    
then after this i am not really sure what to do :

divide it up, and turn the substrings back into integers with int() to do the addition.?


#2

Hello,

Here's what I did. Hope this helps!

def digit_sum(n):
    number = str(n)
    total = 0
    for i in number:
        total += int(i)
    return total
  • You turn the entire number entered into a string
  • Set the total equal to zero
  • Now what you have to do is go through each "character" i.e. number of the "string" i.e. the entire number. To do this, a for loop works the best.
  • Finally for each number you iterate through you are going to change it back into an integer so that we can add it to our total, which was initially set at 0
  • After the for loop goes through each integer of the entire number we entered into the function then we have our grand total! So let's poop it out.

:sunglasses::sunglasses::sunglasses::sunglasses::sunglasses::sunglasses::sunglasses::sunglasses:


#3

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