<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#
<In what way does your code behave incorrectly? Include ALL error messages.>
I am trying to figure out how to get the digit sum using primarily modulos and floor division… I have figured out how to get the digit sum for “less than ever factor of ten” (n<10000 sum=) but to combine it into a unified function is troubling me. Here is what I have:
<What do you expect to happen instead?>
def digit_sum(n):
if n<10:
sum=n%10
return sum
elif n<100:
sum= (n%10)+(n//10)
return sum
elif n<1000:
sum = (n%10)+((n//10)%10)+((n//100)%10)
return sum
elif n<10000:
sum = (n%10)+((n//10)%10)+((n//100)%10)+(n//1000%10)
return sum
elif n<100000:
sum = (n%10)+((n//10)%10)+((n//100)%10)+(n//1000%10)+(n//10000%10)
return sum
Obviously if I won’t be able to “elif:” faster than the computer can think of large words. Does anyone know how I can code (sum+=((n//10**x)%10)
Ideas:
Count *=10
m=len(str(n)
I dont know, help me out
```pythonReplace this line with your code.
<do not remove the three backticks above>