Alright, here goes…
There are three main issues with your code. The first being the fact that you used a while loop instead of a for loop. Using a for loop will help with the next issue I found.
The next issue is that when finding the remainder, you should have been using each digit in
n and not the whole of
n. Using a for loop would fix this issue.
The third, and final “big” issue with your code is the location of your return statement. It should be outside of the loop (I understand why it isn’t there now, it is due to the fact that your while statement will always be true, and putting it outside the while loop would result in an infinite loop. However, the way it is now, the code within your while loop only runs once, whereas, it should run per digits in
n. This is yet another reason why you should be changing your while loop to a for loop).
If you make all of the suggested changes, your code should look something like this:
i = 1
total = 0
for x in str(n):
a = int(x) % ((10 ** i) // (10 ** (i - 1)))
total += a
If you do the things I mentioned, your code should work.