Skip to content

Instantly share code, notes, and snippets.

@CesarNog
Created January 20, 2015 18:25
Show Gist options
  • Save CesarNog/2ac3f092570c97843413 to your computer and use it in GitHub Desktop.
Save CesarNog/2ac3f092570c97843413 to your computer and use it in GitHub Desktop.
Function that takes a positive integer n as input and returns the sum of all that number's digits. For example: digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4.
def digit_sum(n):
total = 0
numberStringfied = str(n)
listNumbers = list(numberStringfied)
for n in listNumbers:
total += int(n)
return total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment