Digit_sum function take exactly one argument


#1



Hi,
Any idea why the following message appears:

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.

Thanks


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


Note the code works fine with IDLE


Replace this line with your code. 
def digit_sum(n):
    total = 0   
    s = []    
    s[:] = list(n)  
    for x in s:
        total = int(x) + total 
    return total

number = input("Enter a number: ") 
print(digit_sum(number))


#2

The program is meant to work with the parameter n, but looking through your loop function, it isn't.

So many issues, too. Please, look through the instructions. :slight_smile:


#3

Thx for your response, I thought my function digit_sum took the parameter n.

I rewrote the exercise with the following code which works fine:
def digit_sum(n):
numberz = []
x = str(n)
for digit in x:
numberz.append(int(digit))
print ("DEBUG: numberz", numberz) # returns numberz [9, 8, 7]
print (sum(numberz)) # returns 24
return sum(numberz)

number = input("Enter a number: ") # eg 987
digit_sum(number)


#4

Okay! :slight_smile:


#5

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