Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,
I have this function that should return every digit of the input added together, but it doesn't work for certain numbers. Is there something wrong with it, or is it the compiler-ish-thing-I'm-using's (I'm on the codewars.com's python section, there are just different challenges to complete if you don't know) fault?
Python
def digital_root(input):
    string = str(input)
    number = 0
    for num in string:
        number+=int(num)
    return number


Thanks
Posted
Updated 14-May-15 22:47pm
v3
Comments
Richard MacCutchan 30-Jan-15 5:57am    
Which numbers does it fail on? Are you sure that each string contains only valid digits?
Rahul VB 15-May-15 4:57am    
In such competitions, you run your code in such an environment, where lots of automatic tests are run on your code. So it is failing in some condition.

What all possibilities you have tried. Or which is the case where it fails? According to my observation, your code takes up a number converts it to string and then sums up all the digits in it.

Why did you convert your number to string and reconverted it back to an int for calculation? - may be thats causing the problem.
ZurdoDev 15-May-15 8:20am    
If you know which numbers fail then just debug it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900