Now, that you have solved it - let me show you a Pythonic solution (works on 3.x only)
PS Actually,
def sum_digits(number): digits_sum = 0 while number: digit, *number = number digits_sum += int(digit) return digits_sum
PS Actually,
while
is less suitable than for
in this casedef sum_digits(number): digits_sum = 0 for digit in number: digits_sum += int(digit) return digits_sumor in the shortest form, just
sum(map(int, number))The latter, of course, takes time to get used to - unless you are a mathematician
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.