I get thrown a math domain error with the code below when it's in code academy. If I run it in a normal python environment then it works perfectly. I'm wracking my brain to figure out what I'm doing wrong here.
For the record, I know the commented version is much much simpler. But I'm still curious as to what I'm doing wrong with the more complicated approach.
''' def digit_sum(n): sumof = 0 n = str(n) for x in range(len(n)): sumof = sumof + int(n[x]) return sumof ''' import math def digit_sum(n): sumof = 0 digits = 0 digits = int(math.log10(n)) + 1 print digits #debug for x in range(digits): sumof = sumof + n // (10**x) % 10 return sumof