def digit_sum(n): last = n // 10( n % 10) first = n / 10 digit_sum = first + last return digit_sum(93) print digit_sum #HINT: #One way might be to convert the integer to a string with str(), iterate over it, and turn the substrings back into integers with int() to do the addition. #If you're looking for a challenge, try this: to get the rightmost digit of a number, you can modulo (%) the number by 10. To remove the rightmost digit #you can floor divide (//) the number by 10. (Don't worry if you're not familiar with floor division—you can look up the documentation here. Remember, this is a challenge!) #Try working this into a pattern to isolate all of the digits and add them to a total.
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question