Digit_Sum


#1



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


I just want to know that if this is the correct solution to this problem

I had some experience in 'C' programming so basically I just changed the syntax to suit python


def digit_sum(n):
    strings =str(n)
    total=0
    a=len(strings)
    number =int(strings)
    while a>=0:
        b=number%10
        total +=b
        number=number//10
        a -=1
    return total


#2

here:

strings =str(n)
number =int(strings)

you convert from integer to string back to integer, why? Its pointless

the order thing i wouldn't do is:

a=len(strings)

why would you do this? given you do floor division on number you can just check if number is greater then zero as while loop condition

this way, your code involves math, which is fast and a good solution


#3

A post was split to a new topic: Digit sum


#4

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