4.Digit_Sum


#1



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


Traceback (most recent call last):
File "python", line 10, in
File "python", line 4, in digit_sum
TypeError: 'int' object is not iterable

Oops, try again. Does your digit_sum function take exactly one argument (a positive integer)? Your code threw a "'int' object is not iterable" error.


It should work. It decode all the stuff in the function and append it to my list


def digit_sum(number):
    numbers = []
    total = 0
    for key in abs(number):
        numbers.append(key)
    for stuff in numbers:
        total += stuff
    return total
    
print(digit_sum(1234))


#2

this line:

for key in abs(number):

number is a integer, you can't loop over it. If you want to do this, you will need to cast it to a string (using: str() ), you can loop over a string. Don't forget to cast back to a int when adding it to total


#3

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