4. digit sum - wrong return value


#1



4. digit sum


This is the error msg i get when my input it 434:
" Your function fails on digit_sum(434). It returns 22 when it should return 11."

I have put the code to python visualizer for a few times and it always showed that the return value is 11, so i have no idea what to do, thanks for any help!


data = []
def digit_sum(n):
    cislo = str(n)
    for char in cislo:
        char = int(char)
        data.append(char)
    return (sum(data))
 
digit_sum(434)


#2

the exercise calls only the function, and because you declare data outside your function, the list isn't emptied, which means it will contain leftovers from earlier function calls, place data inside your function


#4

it worked, and then your delete your reply? But it does work?


#5

yeah it worked. sorry, i wanted to delete the whole topic


#6

You can't delete whole topics, it may contain valuable content for other learners. I will close it, then no one can reply to it anymore, but other students can read the answers in this topic to help them further


#7