Python: Int not iterable error
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)
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))
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)
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.
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.
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.
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.
精彩评论