954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why is this not working?

Im trying to figure out how to compute a product of ordered pairs. So if I have two sets (1,3) and (2,4), I want to generate the following tuples (1,2), (1,4), (3,2), (3,4).

Now I wrote the following

def orderedproduct(set1, set2):
op=[set()]
for x in set1:
for y in set2:
op.add((x,y))
return op

Now, python has a problem with the second to last line because everytime I make two sets and run the program, it says that AttributeError: 'list' object has no attribute 'add'

Clearly something is breaking down with that line, but is it the line itself or is there something Im not seeing in the program?

Edit: Im not sure why my tabbing isnt working here but there should be 1 tab on the 2nd and 3rd line, 2 tabs on the 4th, 3 tabs on the 5th, 1 tab on the 6th.

OLer94
Newbie Poster
7 posts since Sep 2007
Reputation Points: 10
Solved Threads: 1
 

Try append instead of add. Any tutorial or on-line book can help you with lists and how to add/append items.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 
Try append instead of add. Any tutorial or on-line book can help you with lists and how to add/append items.

Woops, I missed that! It works now. Thanks for tip. Sometimes these things are staring you right in the face :$

OLer94
Newbie Poster
7 posts since Sep 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You