-2

I am an newbie in python, while running this code it gives me this error "TypeError: 'int' object is not iterable" in line 14. Please let me know my mistake.

x=int(raw_input())
y=int(raw_input())
z=int(raw_input())
n=int(raw_input())
xarr = [ i for i in range(x+1) ]
yarr = [ j for j in range(y+1) ]
zarr = [ k for k in range(z+1) ]
results = []
for i in xarr:
    for j in yarr:
        for k in zarr:
            results.append([i,j,k]) 
for w in range(len(results)+1):
    if (sum(w) != n):
        results=results.append(sum(w))
print results

if x=1, y=1, z=1 and n=2 then results = [[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]] and I am checking sum of each index of the list results and if the (sum==n) then do not add that index in results list

So output should be : [[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]

4
  • Can you show some code? Commented Mar 14, 2016 at 9:19
  • 1
    Like the error says, w is an integer, and you can't call sum() on an integer; what would it sum? It's not clear what you're trying to do, though; you should state what you expect the result to be. Commented Mar 14, 2016 at 9:20
  • So what is your expected output? The indices of the triples that sum to n? That'd be [0, 1, 2, 4, 7] for your sample. Commented Mar 14, 2016 at 9:45
  • Possible duplicate of TypeError: 'int' object is not iterable in python Commented Mar 14, 2016 at 11:51

2 Answers 2

2

You are summing an integer:

for w in range(len(results)+1):
    if (sum(w) != n):

w is an integer from the range(len(resultss)+1). If you wanted to sum the triples you produced in results, just loop directly over the results:

for w in results:
    if (sum(w) != n):

although it is not clear to me why you are looping over the length plus one. Python indices are 0-based, the last index is at len(results) - 1, not len(results).

You are making it harder on yourself than is needed. Python has several easier options you are missing here.

  • list.append() returns None. You are replacing results with None here:

    results = results.append(sum(w))
    

    Remove the results = part altogether.

    And are you certain you want to combine the (x, y, z) triplets with the sum() results in one list?

  • When using a list comprehension that does nothing but loop and copy the elements to a list ([i for i in iterable]), just use list() on the iterable:

    xarr = list(range(x + 1))
    

    Since all you do is iterate, you don't actually have to do this at all. And there is rarely a need to convert range() objects to lists, you can address individual indices directly without doing so. Just use your range() objects directly:

    xarr = range(x + 1)
    
  • You can use the itertools.product() function to produce your results:

    from itertools import product
    
    results = product(range(x + 1), range(y + 1), range(z + 1))
    

    Again, no need to convert this to a list since you are going to iterate over this.

  • If you wanted to add indices of the triplets, use enumerate() to produce those together with the triples so you can test the sum, then add the index to a result list:

    indices = []
    for i, w in enumerate(results):
        if (sum(w) != n):
            indices.append(i)
    

You can put everything into a one-liner after this, using a list comprehension:

from itertools import product

x = int(raw_input())
y = int(raw_input())
z = int(raw_input())
n = int(raw_input())

indices = [i for i, w in enumerate(product(range(x + 1), range(y + 1), range(z + 1)))
           if sum(w) != n]
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to use range here

"range(len(results)+1)"

As results will be a list of list [[x,y,z], [x,y,z]].

Try this:

for w in results:
    if (sum(w) != n):
        results=results.append(sum(w))

Or do like this:

for w in range(len(results)+1):
    if (sum(results[w]) != n):

1 Comment

There are some other major problems in that code. results=results.append() will lead to a AttributeError: NoneType has no attribute 'append', for example.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.