Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!! PLEAE DO

ID: 3905263 • Letter: P

Question

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

PLEAE DO PART B AND C AS I HAVE ALREADY DONE A!!!!!!!!!!!!!!!!!!!!!!!!!!!

4. Tuple Input a) Write a function called input tuple (in a new file p4py) 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 throush the types tuple and will try to parse the inout string based on the types The function takes the following parameters prompt: a string to be displayed as input prompt 2. types atuple of type objects that must match the types of the data entered by the user and returmed by the function. The only types supported are int floot, str, boo 1 sep: a string that separates the input objects, with default value The function returns one ol ifparsing the data from the user acording 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 () · Error handling the function must not raise/throw emors related to parsing or input/read operations Use proper error handling with try/except.h se o' any error, print an error message to the terminal nd return the empty tuple (0 Hore is a surnesful eample on how thr function can be used: stutent tup -input tupieEnter tirz namelax nme, zReotio l, tuizime Ibool Istr, str, toa. nt, ool), ?this is the tuple with expected types is the separatee charonef on the inde ine The function displays the prompt: Enter first name, last name, age a, D ), ulltine (ol The user enters the string: Sponeebob,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 funsiun call is the sare as above, but the user entess: "Spongebab S Notce that the token count is less than required - a separator is missine, The function retuns Another example ot failure is if the user types: "KeitnRupert,Murdoch,20,10010 false". The function exgects a float for the 3 object, butgets instead a string that cant be converted to tloat. The functiun returre (1 too True" Functiun input tuple should NOT use list comprehersions Hint: Python types pike int, strl can be used as constructors to construct an object of a carrmponding type ro nside a string Far exaplt, n"0)returns cbject of type irt with number 30 b Write a function called input tupir k that is identical to input tuple except that it uses list Write a function reo tuple that works sinarly to input tuple, but instead o reading input from the terminal, t radi trot from ? fir otprct pasn d as argument-rf this funct on u carrectly a st comprehension you aet 2 extra points The tunction reod tuple takes the tollowing parameters 1. file obj: an obiject represeniting an apen text fie: e.g aperied with open"Filenarne"r. 2. types a tuple of type obiects that must match thr types of the datn entered by the usrr and retumed by the function. The only types supported are int float, str, boo 3, sepr: astring that srparabes the input objcts, with default valu The function returns one of: If parsing the data from the user accordine to the types tuple succeeds, then it returns the tuple with the converted data . if parsing the data from the user accordng to the types tuple fails, then it returns the empty tuple() Error handling: the function must nat raine/throw errors related to parsing or inputfread operations Use proper error handling with trylexcept in case of any error, print an error message to the terminal and return the empty tuple A usage scenario [omitting error handing for brevity)would be: maie, model, mpc flot, modelvrine, newer boo)- read tuplelE t str, flast, int boell,Iuse thme varabls fcloe) This code will work fine if the file cars.csv looks like this: lodo, Nivo,195,1957,Folse Porsche,911 Turbo 5,17.5,18, Folse Function read tuple) should return in this case the tuple l'lada,Niva,19.5,1987, Falsel for the first time it's called. If the input format does not comply with types argurnent Istr, str, float, int, boo) then the furnction shaul c) In the main program (p4.py)write code that tests the functions from a) and bl. Hints notice that Ms word uses a special type of the double quote-characters, in case you want to paste to a Python edkor .

Explanation / Answer

def input_tuple_ls(l,t):
    rlist = []
    try:
      print(l)
      inp = input("")
      list2 = inp.split(",")
      if len(list2) != len(type):
         raise ValueError
      else:
         for i in range(len(list2)):
             if isinstance(list2[i],t[i]):
                rlist.append(list2[i])
             else:
                raise ValueError

         return rlist
    except:
      print("Invalid input")
      return []


def read_tuple(f,t):
    rlist = []
    try:
      for line in f:
          list2 = line.split(",")
          if len(list2) != len(type):
             raise ValueError
          else:
             for i in range(len(list2)):
                 if isinstance(list2[i],t[i]):
                    rlist.append(list2[i])
                 else:
                    raise ValueError

         return tuple(rlist)
    except:
      print("Invalid input")
      return []