In: Computer Science
ON PYTHON:
a) Write a function named concatTuples(t1, t2) that concatenates two tuples t1 and t2 and returns the concatenated tuple. Test your function with t1 = (4, 5, 6) and t2 = (7,)
What happens if t2 = 7? Note the name of the error.
b) Write try-except-else-finally to handle the above tuple concatenation problem as follows:
If the user inputs an integer instead of a tuple the result of the concatenation would be an empty tuple. Include an appropriate message in the except and else clause to let the user know if the concatenation was successful or not. Print the result of the concatenation in the finally clause.
(a).
def concatTuples(t1,t2):
return t1+t2
Input 1:-
print(concatTuples((4,5,6),(7,)))
Output:-
(4, 5, 6, 7)
Input 2:-
print(concatTuples((4,5,6),7))
Output:-
TypeError: can only concatenate tuple (not "int") to tuple
------------------------------------------------------------------------------------------------------
(b).
def concatTuples(t1,t2):
try: #This block check if any error occurred in the enclosed code.
t3 = t1+t2
except: #If error occurs , this block is executed not else block
t3 = ()
print('Concatenation is not succesfull as one argument is an integer')
else: # If no error occurs, this block is executed.
print('Concatenation is Successful')
finally: # This block is always executed
print(t3)
Input 1:-
concatTuples((4,5,6),(7,))
Output:-
Concatenation is Successful
(4, 5, 6, 7)
Input 2:-
concatTuples((4,5,6),7)
Output:-
Concatenation is not succesfull as one argument is an integer.
()