Write a function called input_tuple that reads from the terminal a sequence of o
ID: 3794995 • Letter: W
Question
Write a function called input_tuple that reads from the terminal a sequence of objects with types provided by a tuple given as parameter and that returns the sequence of objects read as a tuple. The function will iterate through the types tuple and will try to parse the input string based on the types.
Error handling: the function must not raise/throw errors related to parsing or input/read operations. Use proper error handling with try/except. In case of any error, print an error message to the terminal and return the empty tuple ().
Here is a successful example on how the function can be used:
student_tup = input_tuple(“Enter first name, last name, age (float), ID (int), fulltime (bool): “,
(str, str, float, int, bool), # this is the tuple with expected types “,”)
# , is the separator character on the input line
The function displays the prompt:
“Enter first name, last name, age (float), ID (int), fulltime (bool): “ The user enters the string: “Spongebob,Squarepants,23.5,1,True”. Then, the tuple returned will be equal to (“Spongebob”, “Squarepants”, 23.5, 1, True).
Here is an example when parsing based on the types tuple fails:
The function call is the same as above, but the user enters: “Spongebob Squarepants,23.5,1,True”. Notice that the token count is less than required – a separator is missing. The function returns ().
Another example of failure is if the user types: “Keith,Rupert,Murdoch,20,10010,False”. The function expects a float for the 3rd object, but gets instead a string that can’t be converted to float. The function returns (), too.
Function input_tuple should NOT use list comprehensions.
Hint: Python types (like int, str) can be used as constructors to ‘construct’ an object of a corresponding type from a string. For example, int(“30”) returns object of type int with number 30 inside.
b) Write a function called input_tuple_lc that is identical to input_tuple except that it uses list comprehension(s).
c) Write a function read_tuple that works similarly to input_tuple, but instead of reading input from the terminal, it reads text from a file object passed as argument. If this function uses correctly a list comprehension you get 2 extra points.
Error handling: the function must not raise/throw errors related to parsing or input/read operations. Use proper error handling with try/except. In case of any error, print an error message to the terminal and return the empty tuple ().
A usage scenario (omitting error handling for brevity) would be: f = open(“cars.csv”, “r”) (make, model, mpg_float, modelYr_int, newcar_bool) = read_tuple(f, (str, str, float, int, bool), “,”) # use these variables …. f.close()
This code will work fine if the file cars.csv looks like this: Lada,Niva,19.5,1987,False Porsche,911 Turbo S,17.5,1989,False Function read_tuple() should return in this case the tuple (‘Lada’,’Niva’,19.5,1987,False) for the first time it’s called. If the input format does not comply with types argument (str, str, float, int, bool) then the function should return ().
d) In the main program (p1.py) write code that tests both functions. Using the testif function from the Unit 2 module for writing your tests gives you 2 extra credit points.
The function takes the following parameters: prompt: a string to be displayed as input prompt types: a tuple of type objects that must match the types of the data entered by the user and returned by the function. The only types supported are int, float, str, bool sep: a string that separates the input objects, with default value".". . The function returns one of: if parsing the data from the user according to the types tuple succeeds, then it returns the tuple with the converted data to Python objects, · if parsing the data from the user according to the types tuple fails, then it returns the empty tuple ()Explanation / Answer
class InnerTryCatch
{
static void div(int a, int b, String s)
{
System.out.println("1");
try
{
System.out.println("2");
try
{
int c = a / b ;
System.out.println(c);
}
catch(ArithmeticException ae)
{
System.out.println("Inside inner catch");
}
System.out.println("4");
System.out.println(s.toUpperCase());
System.out.println("5");
}
catch(ArithmeticException ae)
{
System.out.println("6");
}
System.out.println("7");
}
public static void main(String[] args)
{
div(250, 10, "abc"); div(250, 0, "abc"); div(250, 0, null);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.