4/15 digit_sum - "string indices must be integers, not str"


#1



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


It threw "string indices must be integers, not str" error, and I don't see what is incorrect in my code.


def digit_sum (n):
    string = str(n)
    count =0
    for i in string:
        count = int(string[i]) + count
    return string.

Using count=+int(string[i]) instead of my way do not change anything.


Please, show my my mistake.


#2

you should cast i to a integer, not the whole string of numbers.


#3

I'm sorry, but I didn't understand that. Can you clarify?


#4

the variable i will hold the number you want to add to count, so you should cast i to a integer, not string[i]


#5

Thank you, I guess what I did wrong, is that I assumed, that "i" inside for-loop should be the index and not the value.


#6

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