Answer to Question #301655 in Python for Sudheer

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.

1
Expert's answer
2022-02-23T06:23:47-0500
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS