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

(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,...
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
(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...
Make a python code. Write a function named max that accepts two integer values as arguments...
Make a python code. Write a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Write the program as a loop that...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one...
Write the following easy Python functions: 1) Write the function named roundDollars(). The function has one input, a String named amountStr which consists of a dollar-formatted amount, such as "$ 1,256.86". The returned value is an int representing the number of rounded "dollars" in the amount, (1257 in the sample shown here). You will need to scrub, format and parse the input, then use arithmetic to determine how many rounded dollars the amount contains. roundDollars("$ 1,256.86") → 1257 roundDollars("$ 0.42")...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT