Digit_nums with floor division


#1



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


Code works fine, but i'm wondering how the floor division should look like, or is my code correct?


n = int(raw_input( "Number pls: "))

def digit_sum(n):
    summe = 0
    if n < 10:
        return n
    while n >= 10:
        var = n % 10
        n = n // 10
        summe += var
        if n < 10:
            summe += n
    return summe
    
print digit_sum(n)


#2

Hi, @19striker05 ,

The // operator performs floor division, and you did it correctly here ...

n = n // 10

Another way to implement the above is ...

n //= 10

#3

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