Question: USE PYTHON3; DO NOT IMPORT ANY PACKAGES Please do both parts a and b of question 1 and follow the directions carefully. Thank you! 1.

USE PYTHON3; DO NOT IMPORT ANY PACKAGES

Please do both parts a and b of question 1 and follow the directions carefully. Thank you!

1.

a) For this question we will be checking the correctness of input1 and input2 using exceptions and NOT assertions. If a check fails, you will throw an exception. You should make sure you are doing the following checks in order to prevent issues with the doctests expecting the wrong exception.

Checks (in this order):

  1. input1 should be a list

  2. All of the values in input1 should be numeric. It is ok if input1 is empty

    1. If there are multiple non-numeric values, you only need to throw an exception for the first non-numeric value encountered

  3. input2 should be numeric

  4. input2 should be contained in input1

If all of the checks pass, you should return the string 'Input validated'

Refer to the doctests to see which exceptions are thrown for each of the checks and how the corresponding messages should be formatted.

def check_inputs(input1, input2):

"""

>>> check_inputs([1, 2.0, 3.0, 4], 4)

'Input validated'

>>> check_inputs([], 1)

Traceback (most recent call last):

...

TypeError: input2 not in input1

>>> check_inputs(1, 1)

Traceback (most recent call last):

...

TypeError: input1 is not the correct type

>>> check_inputs([1, 2, 'hi'], 4)

Traceback (most recent call last):

...

TypeError: The element at index 2 is not numeric

>>> check_inputs([1.0, 2.0, 3.0], 'hello')

Traceback (most recent call last):

...

TypeError: input2 is not the correct type

"""

return ...

b)

For this question we will be checking the correctness of a given filepath and its corresponding file using exceptions and NOT assertions. If a check fails, you will throw an exception. You should make sure you are doing the following checks in order to prevent issues with the doctests expecting the wrong exception.

Checks (in this order):

  1. filepath should be a string

  2. filepath should be a valid filepath (i.e. we can open the file)

  3. The file at filepath should not be empty (i.e. there should be >= 1 words in the entire file)

If all of the checks pass, you should return the number of words in the file.

Refer to the doctests to see which exceptions are thrown for each of the checks and how the corresponding messages should be formatted.

def load_file(filename):

"""

>>> load_file(1)

Traceback (most recent call last):

...

TypeError: filename is not a string

>>> load_file('files/ten_words.txt')

10

>>> load_file('files/empty.txt')

Traceback (most recent call last):

...

ValueError: File is empty

>>> load_file('files/nonexistant.txt')

Traceback (most recent call last):

...

FileNotFoundError: files/nonexistant.txt does not exist

"""

return ...

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!