-4
import sys
print (sys.version) 

def add_pair((a,b),(c,d)):
    return a+c, b+d

print (add_pair((10,20),(30,40)))

This works perfectly in python2.x but not on python3.x

Austin
  • 25,759
  • 4
  • 25
  • 48
Syed Ikram
  • 67
  • 1
  • 3
  • 5

3 Answers3

2
import sys
print(sys.version)

def add_pair(x,y):  
    ''' Unpack the tuples here e.g  a, b = x, c, d = y or as below (as per the req)''' 
    return(x[0]+y[0], x[1]+y[1])

print(add_pair((10,20),(30,40)))

Tuple parameters are no longer support in Python3: http://www.python.org/dev/peps/pep-3113/

You have to unpack the tuples before doing any operation in the function.

Chandu
  • 2,053
  • 3
  • 25
  • 39
0

I'm surprised it supposedly works in 2.x, but apparently explicit tuples were ok in function arguments. But this was never great style. Function arguments should just be separated by commas. This works in both:

def add_pair(pair1, pair2): 
    a, b = pair1
    c, d = pair2
    return a+c, b+d

print(add_pair((10, 20), (30, 40)))
chryss
  • 7,459
  • 37
  • 46
0

They removed the unpacking of tuple parameter in python3. So alternative way of doing it is like this or mentioned in above answers :

def add_pair(*kwargs):
    return tuple(sum(i) for i in zip(*kwargs))

print(add_pair((10,20),(30,40)))
# (40, 60)
Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33