Digit_sum() with %


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#

can someone please help me? i don’t know why it’s not working . it’s just give me ones of my number.

```


def digit_sum(n):
    i = 1
    total = 0
    while n % (10 ** i) > 0:
        a = (n % (10 ** i)) // (10**(i-1))
        total += a
        i= i + 1
        return total
print digit_sum(1234)

#2

You are overthinking this. Try following this template:

def FUNCTION(PARAMETER):
    VARIABLE = 0
    for INDIVIDUAL_ITEM in str(PARAMETER):
        VARIABLE += int(INDIVIDUAL_ITEM)
    return VARIABLE

Make sure not to just follow the template and move on. I want to make sure that you understand how and why this works. If you have any questions, just ask.


#3

thank you. i do undrestand how that template works but the first idea that came to my mind was the one i wrote and i want to make that work.


#4

Fair enough. Give me a moment to review your code again…


#5

May I ask you a question? What are you trying to accomplish with n % (10 ** i)? As far as I can tell, it doesn’t make much sense to find the remainder of n / 10ⁱ? Walk me through your thinking.


#6

i want to get my number’s digits via getting remainder of my number by powers of ten.i’d made a mistake and i chenged my code a bit ،. could you take a look at it?


#7

Certainly. Just post your updated code and I will take a look.


#8

i edited my first post.thanks in advance


#9

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:

def digit_sum(n):
    i = 1
    total = 0
    for x in str(n):
        a = int(x) % ((10 ** i) // (10 ** (i - 1)))
        total += a
    return total

print digit_sum(1234)

If you do the things I mentioned, your code should work.

MattSmith


#10

Upon further review, though the code I showed you works, but it still has many unnecessary parts. After removing them, your code looks awfully familiar… :wink:

def digit_sum(n):
    total = 0
    for x in str(n):
        total += int(x) % 10
    return total

#11

Sorry to change the topic but wasn’t there something with the floor division too?


#12

A post was split to a new topic: Digit_sum


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.