4/15 digit_sum (Practice makes Perfect)


I am trying to do this with the first suggested method ie. converting the integer to a string, but am messing up somewhere and getting a "Your code threw a "'str' object is not callable" error"

def digit_sum(n):
# Convert integer to a string
    nstring = str(n)
# Initialize the total
    total = 0 
#Loop through each substring letter
    for c in nstring:
#Convert each substring letter to an integer and increment the total
        total += int(c(nstring))
    return total


The problem is on this line

total += int(c(nstring))

when you created the for loop like this

for c in nstring

an iterator called c was created over the elements in nstring and this iterator has the same types as each element in nstring.Which implies that c at each step of the iteration is each character in nstring.
Now when you wrote


you are trying to call c as you would a function but it is not that's why you get that error.
What you need to do is just add ,the converted character, c in nstring to total


Thanks- I simplified the penultimate line and now it works.