#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Trouble using the sum function..


    This is my code:
    x = [1,2,3,4,5,6,7,8,9]

    total = sum(x)
    print total

    This is my error report:
    Traceback (most recent call last):
    File "python", line 3, in <module>
    TypeError: 'int' object is not callable


    I want to simply take the sum of the items in my list named x by using the sum function. What am I doing wrong and a correct way to do this would be appreciated.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    It's strange that it doesn't work for you. There's nothing wrong with your code. I don't think the sum() function is limited to any one version, but I suppose something could be off - so which version of Python are you using?

    Code:
    >>> x = [1,2,3,4,5,6,7,8,9]
    >>> total = sum(x)
    >>> print total
    45
    >>>
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Forgive and correct me if my vocabulary is incorrect.
    I am using the compiler on a website called codecademy.com

    Maybe the problem is on their end?

    If you want to check it out, here is the majority of the url.. i'm not allowed to post whole ones
    codecademy.com/courses/python-beginner-en-qzsCL/1#!/exercises/0/scratchpad
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0
    Actually, there are no problems now. I didn't change anything either. Weird. Thanks for your time.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    No problem. Glad things worked out.

    Good luck with Python

IMN logo majestic logo threadwatch logo seochat tools logo