Question

In: Computer Science

On Python: a) Write a function named concatTuples(t1, t2) that concatenates two tuples t1 and t2...

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.

Solutions

Expert Solution

ANSWER :

GIVEN THAT :

(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.
()


Related Solutions

ON PYTHON: a) Write a function named concatTuples(t1, t2) that concatenates two tuples t1 and t2...
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...
Two solid bodies at initial temperatures T1 and T2, with T1 > T2, are placed in...
Two solid bodies at initial temperatures T1 and T2, with T1 > T2, are placed in thermal contact with each other. The bodies exchange heat only with eachother but not with the environment. The heat capacities C ≡ Q/∆T of each body are denoted C1 and C2, and are assumed to be positive. (a) Is there any work done on the system? What is the total heat absorbed by the system? Does the internal energy of each subsystem U1 and...
Suppose T1 and T2 are iid Exp(1). What is the probability density function of T1+T2? What...
Suppose T1 and T2 are iid Exp(1). What is the probability density function of T1+T2? What is the probability that T1+T2 ≥ 3?
If T1 , T2 : Rn → Rm are linear maps with range(T1) = range(T2), show...
If T1 , T2 : Rn → Rm are linear maps with range(T1) = range(T2), show that there exists an invertible linear map U:Rn →Rn so that T1=U◦T2
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Starting with "m1g - T1 = m1a ", "T2 - m2g = m2a" and "(T1 -...
Starting with "m1g - T1 = m1a ", "T2 - m2g = m2a" and "(T1 - T2) R = I ?" show the derivation steps of "(m1 - m2)g = (m1 + m2 + I/R^2) a
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is a palindrome, and boolean False if it is not. The palindromes for this problem are contained in the list below. You must use this list in your solution. Use a for loop to retrieve each palindrome and test it. Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list....
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT