4. Digit_sum - Why my code wont run?



I am wondering why the code I wrote wont work. If I manually (without using a for loop) enter the instructions in python it seems to work fine...
What I am attempting to do with the code is:
To convert my int to str so I can access the different numbers and then use a for loop to go one by one adding the values which I am converting back into int

I appreciate any insight! Cheers :slight_smile:
The error message I get is : "string indices must be integers, not str"

Replace this line with your code. 
def digit_sum(x):
    y = str(x)
    result = 0
    for z in y:
        result += int(y[z])  
    return result


z contains values from the list, not indexes


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