4. Digit_sum - Why my code wont run?


#1



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


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


#2

z contains values from the list, not indexes


#4

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