Created
January 20, 2015 18:25
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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