Question #301655

writing a python function Add_Values(a,b) (Refer RollNo_W10A_2.py)which takes either two integer or float or string input parameters and perform addition/concatenation if the type of both parameters is the same otherwise give an exception as shown in the example.

Expert's answer

def Add_Values(a, b):
    if type(a) == type(b):
        return a+b
    else:
        return 'a and b have different data type'

print(Add_Values(1, 'str'))          # a and b have different data type
print(Add_Values(1, 5))              # 6
print(Add_Values(2.3, 6))            # a and b have different data type
print(Add_Values('str1', 'str2'))    # str1str2

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS