Digit_sum


#1

Hi, I did this code:

def digit_sum(n):
  total = 0
  for n in str(n):
    total += int(n) % 10 // 10
  return total

because in hint it says To remove the rightmost digit you can floor divide (//) the number by 10.
Where gone wrong? Thanks


Digit_sum() with %
#2

you are pretty close to the solution:

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

its your code except i removed % 10 // 10

honestly, if you want to for the challenge (as described in the hint), you should get rid of str(), int() and for, they are not suitable for this solution

the great thing about the challenge solution is that involves only math, not using string/integer conversion.

given we keep removing the right most digit we can use a while loop until we are through the digits


#3

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