0

I have written the following code

  p1=1
  p2=2
  p3=set(p1).union(set(p2))
  p3

In this case I would have expected for p3 to be equal to {1,2}, but when trying to run the program, I obtain:

'int' object is not iterable

How may I fix this?

Is there also a way to print the answer as an ordered pair? such as (1,2) in this case.

I have changed the code a little, because this is what I would like to fix, since having this step solved, will let me know how I do the rest.

Thanks in advance

divibisan
  • 11,659
  • 11
  • 40
  • 58
Stiven G
  • 215
  • 2
  • 7
  • `m={}` defines a dict literal. You want `m = set()` for empty set. – Moses Koledoye Aug 26 '18 at 14:09
  • How is your set subscription even possible i.e. `v[k]` and `w[l]`. Sets are not ordered. You want to iterate directly on the sets. – Moses Koledoye Aug 26 '18 at 14:31
  • You are right Moses, I actually have a longer code which takes care of that but did not posted it. I believe my issue is somewhat simple, but don't see how may I really take care of the union when just looking for the integers inside the { }, – Stiven G Aug 26 '18 at 18:01

1 Answers1

0

I saw what my mistake was. what I needed was a pair of [].

The correct code is:

  p1=1
  p2=2
  p3=set([p1]).union(set([p2]))
  p3
Stiven G
  • 215
  • 2
  • 7