"str object is not callable" please help


#1



Hello evryone i'm stuck please help :slight_smile:
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096


I get "Your function crashed on 434 as input because your function throws a "'str' object is not callable" as an error i read about someone having the same error (not on code academy) about redifinning what str ment so i assume the error is when i convert n from int to a string.


Replace this line with your code. 
def digit_sum(n):
     if n > 0:
         n = str(n)
         list(n)
         n (int, n)
         return sum(n)
     else:
        print "Your number is not positive sorry"

Thanks in advance,


#2

This is the problem. I am not sure what you are trying to do in that line, so I can't really suggest anything better.


#3

Hello :smile:
Was trying to get the average of n I think. (forgot about the sum command) `

def digit_sum(n):
    n = str(n)
    list(n)
    sum(n)
    n = int(n)
    return n

However i'm still getting errors. ". 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." Do you know where this could come from? Thanks a lot for helping btw.


#4

Here you turn the string n into a list of strings with length one.

You can't sum a list of strings, that only works with numbers. Also you need to store the sum somewhere, e.g. total = sum(integer_list).

You can't turn list into integers, you need to turn all the strings in the list to into integers.


#5

A string is iterable. We can build a list of integers directly from that.

def digit_sum(n):
    return sum([int(x) for x in str(n)])

#6

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