Python Error: Int not itable

I'm trying to get my python feet wet on Project Euler, but I have a problem with the first problem (find the sum of multiples of 3 or 5 to 1000). I can successfully print multiple of three and five, but when I try to enable the sum function, I get the following error:

TypeError: object 'int' is not iterable

Any help would be greatly 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)
+5
source share
6 answers

In for-loop

for numbers in x:

"numbers" pass through the elements x one at a time for each passage through the cycle. It might be better to name the variable "number" because you only get one number at a time. "numbers" are an integer every time through a cycle.

sum(numbers)

TypeError, sum() (, ), .

, :

def numcheck(x):
    s=0
    for number in x:
        if number%3==0 and number%5==0:
            s+=number
    print(s)
numcheck(range(1000))
+7

, sum(). - x.

- :

numbers = [num for num in x if num%3==0 and num%5 ==0]
print sum(numbers)
+5

sum , .

for numbers in, numbers . print, , numbers - .

, 3 5 . , , sum .

+1

, - .

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

, sum() .

+1

() sum :

(...)   sum (sequence [, start]) →

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)

, int sum(), .

+1

:

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

, xrange (n) . , n , numcheck. C. , xrange - , , .

+1

All Articles