Hi,

I have a program:

def AdjacencyMatrix(dim, pairs):
    matrix1 = (zeros((dim,dim)))
    for (first,second) in pairs:
        matrix1[first,second]=1
        matrix1[second,first]=1
    print '\nAdjacency Matrix\n'
    print matrix1

But I get an error:

TypeError: 'int' object is not iterable

Can anyone help? Thank you!

First, you need to use code tags on your code to make it readable and not lose your inentation.

Your line for (first, second) in pairs: is along with your error suggesting that pairs is being passed in as an integer.

When you provide an example code it would help if it is an actual example that can be run to demonstrate your dilemma. Otherwise we are left to guess at best, which is all we can do here since we can't see what you're passing to this function or what the context of your program is like.

You can test if an object can be iterated over ...

import operator

a = 1
b = 'abc'
c = [1, 2, 3]
d = {'a': 1, 'b': 2, 'c': 3}

print operator.isSequenceType(a)  # False
print operator.isSequenceType(b)  # True
print operator.isSequenceType(c)  # True
print operator.isSequenceType(d)  # False
print operator.isSequenceType(d.items())  # True

# test if an object is a sequence and can be iterated over
if operator.isSequenceType(b):
    for x in b:
        print x
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.