Digit sum using % and //


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4


this works and makes sense to me .
but it only works to a certain length of digits (that i manually have to type in) .


is there a way to condense this into a loop of some kind ?


def digit_sum(n):
    n = \
    n % 10 + \
    n // 10 % 10 + \
    n // 100 % 10 + \
    n // 1000 % 10 + \
    n // 10000 % 10 + \
    n // 100000 % 10 + \
    n // 1000000 % 10 + \
    n // 10000000 % 10 + \
    n // 100000000 % 10
    return n

# ^^^ simpler way to write this ? ^^^ #

# vvv testing area vvv #

print digit_sum(n)
print ''
print ''
print 1234.5
print 1234.5 % 10
print 1234.5 // 10 % 10
print 1234.5 // 100 % 10
print 1234.5 // 1000 % 10


#2

If you look at what you do for each step - you'll find that there is a change that is applied to the operation and that change is the same each time and is therefore something you can place in a loop, yes


#3

How would we put this into a loop?

>>> n = 1234
>>> s = 0
>>> s += n % 10
>>> n = n // 10
>>> s
4
>>> n
123
>>> s += n % 10
>>> n = n // 10
>>> s
7
>>> n
12
>>> s += n % 10
>>> n = n // 10
>>> s
9
>>> n
1
>>> s += n % 10
>>> n = n // 10
>>> s
10
>>> n
0

#4

oh dude , yes
that helped my brain
now im only limited by MemoryError ' s

def digit_sum(n):
    for s in range(n):
        while True:
            s += n % 10
            n = n // 10
            if n == 0:
                return s

print digit_sum(1234567)

#5

We can remove that loop, altogether. Your conditional loop is all you need. Remember to initialize s.


#6

ah , ok
the syntax took me a second

def digit_sum(n):
    s = 0
    while True:

#7

Well done! I applaud your use of an infinite loop to scratch at all the iterations.

Now see if you cannot find a way to remove the break condition from inside the loop and have only one condition (hint, it's not, True).

Think n, but in the while condition.


#8

always strive for infinity

    while n != 0:
        s += n % 10
        n = n // 10
    return s

# or i suppose : #

    while n >= 1:

not sure if one is more flexible than the other


#9

That is right, on one level, and scarily dangerous on another. It's a NOT identity, not an inequality. I would take this very seriously. It can be NOT ZERO in the negative, too, should our code mistakenly take us into that domain.

Preferrably, and much safer is the inequality,

while n > 0:

Thus n can never be negative.

Moot now, given the above example. But we can rain on this parade, still, given the falsy nature of 0.

while n:

which brings us squarely back in the previous scenario. A lesson learned.


#10

just wanted to make sure negatives were off the board
and also got me thinking what a string of digits (with a random negative sign within it) would do
the positive sign didnt confuse it , though

print digit_sum(12)
print digit_sum(1 + 2)
# ^^^ this works ^^^ # 



# vvv it really doesnt like this vvv #
print digit_sum(1 - 2)
print digit_sum(-12)

#11

A string of signed digits (numbers) is a vector expression. The negatives cancel out the positives and the residual is the number that stands at the outcome.

Expressions are values that have not yet been evaluated. The interpreter, though, has its eye only on the value since it is handed that by the parser, which dutifully evaluated the expression before passing it on.


#12

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