5

I'm attempting to get my feet wet with python on Project Euler, but I'm having an issue with the first problem (find the sum of the multiples of 3 or 5 up to 1,000). I can successfully print out multiples of three and five, but when I attempt to include the sum function I get the following error:

TypeError: 'int' object is not iterable

Any help would be appreciated.

n = 100
p = 0
while p<n:
   p = p + 1
x = range(0, p)

# check to see if numbers are divisable by 3 or 5
def numcheck(x): 
   for numbers in x:
      if numbers%3==0 and numbers%5==0:
          sum(numbers)
numcheck(x)
2
  • 1
    I just ran the code you posted and did not get that error. I see that you edited your code to remove the line that caused the error. What's your question now? Commented Oct 17, 2009 at 20:57
  • Woops, didn't realized I edited that out, one sec. Commented Oct 17, 2009 at 21:06

6 Answers 6

7

In the for-loop

for numbers in x:

"numbers" steps through the elements in x one at a time, for each pass through the loop. It would be perhaps better to name the variable "number" because you are only getting one number at a time. "numbers" equals an integer each time through the loop.

sum(numbers)

throws a TypeError because the function sum() expects an iterable object (like a list of numbers), not just one integer.

So perhaps try:

def numcheck(x):
    s=0
    for number in x:
        if number%3==0 and number%5==0:
            s+=number
    print(s)
numcheck(range(1000))
2
  • Changed accepted answer to yours for providing an explanation as well, thanks (here's hoping I can get the next problem on my own). Commented Oct 17, 2009 at 21:04
  • +1 for suggesting that variable names match what they actually are. It's amazing how many bugs can be prevented just by having clear names.
    – Lee B
    Commented Oct 17, 2009 at 23:02
5

numbers needs to be a list or similar when it is passed to sum(). In the code example above, it is an integer - one of the integers from x.

Try something like:

numbers = [num for num in x if num%3==0 and num%5 ==0]
print sum(numbers)
2
  • 1
    sum takes a iterable so this works too: sum(n for n in x if n%3==0 and n%5==0) Commented Oct 17, 2009 at 21:16
  • That's by far the most Pythonic solution, and a one-liner. Recasting imperative code into comprehensions is a whole worthy subject in itself.
    – smci
    Commented May 13, 2011 at 18:15
1

The sum function expects a list, not a single number.

When you do for numbers in, then the variable numbers has a single integer object. Add a print statement, you'll see that numbers is a single number.

You might want to accumulate all the multiples of 3 and 5 in a list. Once you have the list, you can then use the sum function on that list.

1

I think you want something like what follows.

def numcheck(x):
    total = 0
    for number in x:
        if number % 3 == 0 or and number % 5 == 0:
            total += number
    print total

Alternatively, you could append each of the divisible numbers to a list, and then call sum() on that list.

1
  • 4
    Shame on you for providing the solution.
    – S.Lott
    Commented Oct 17, 2009 at 20:50
1

help(sum) Help on built-in function sum in module builtin:

sum(...) sum(sequence[, start]) -> value

Returns the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0).  When the sequence is
empty, returns start.

(END)

You are passing numbers which is of type int to sum(), but sum takes a sequence.

1

Here is how I would do this:

n = 100

# the next 4 lines are just to confirm that xrange is n numbers starting at 0
junk = xrange(n)
print junk[0]  # print first number in sequence
print junk[-1] # print last number in sequence
print "================"

# check to see if numbers are divisable by 3 or 5
def numcheck(x): 
   for numbers in x:
      if numbers%3==0 and numbers%5==0:
          print numbers

numcheck(xrange(n))

You may find it strange that I pass xrange(n) as a parameter. This is an iterator that will eventually produce the list of n numbers as you go through the loop in numcheck. It's a bit like passing a pointer to a function in C. The key thing is that by using xrange, you do not need to allocate any memory for the list of numbers, so you can more easily run a check on the first billion integers, for instance.

3
  • I'm not sure if I entirely understand xrange yet (I'm not all too familiar with pointers as well), so bare with me. So while range generates a list of, let's say 100 numbers, on the spot, xrange would generate a single number, check if that number matches whatever conditions you've given it, and continue moving on to the next number until it reaches 100? Commented Oct 17, 2009 at 21:38
  • xrange() was invented because sometimes we call range() and we don't actually want the whole list. A for loop in Python always sets its variable to elements from a list (or something other than a list that is "iterable"). Suppose you want to do something a million times. With for x in range(1000000): you build a list of a million integer objects, and then you use it once and discard it. xrange() instead of returning a list, returns an iterator, and the for loop knows how to ask the iterator for one value at a time. No need to build and destroy a huge list.
    – steveha
    Commented Oct 18, 2009 at 6:26
  • 1
    xrange() worked out very well, and in Python 3.x, there is no longer a range() that returns a full list; range() in Python 3.x acts like xrange() in Python 2.x. In Python 3.x if you actually want the list, you simply do this: list(range(1000)) The list type takes the iterator and turns it into a full list. In fact, all the method functions in Python that used to return full lists return iterators in Python 3.x. You can force them to expand to a full list if you want, and when you don't care, the iterator is more efficient.
    – steveha
    Commented Oct 18, 2009 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.