Need Help? digit_sum (Lesson 4)


def digit_sum(n):
    m = str(n)
    final_product = 0
    numbers = []
    for c in m:
    for i in numbers:
        l = 0
        numbers[l] == int(i)
        l = l + 1
    for i in numbers:
        final_product = final_product + i

Got the error message " Oops, try again. Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "unsupported operand type(s) for +: 'int' and 'str'" error." What am I doing wrong?


The above loop is a read-only look-up loop. We cannot mutate a list in a read only loop. numbers[l] will remain a string. As a result, this loop is not doing anything and may be removed.

may be written,

    for i in numbers:
        final_product = final_product + int(i)

As an aside, the letter l is an easily confused variable and should not be used in our code. Lowercase i is at least distinguishable from 1. Typical iterator variables are i, j and k.


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