Digit_sum function take exactly one argument


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.



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: ") 


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:


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:
print ("DEBUG: numberz", numberz) # returns numberz [9, 8, 7]
print (sum(numberz)) # returns 24
return sum(numberz)

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


Okay! :slight_smile:


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