Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Market Research Cost Effective Searching Of The Internet And Online Databases

Authors: John F. Lescher

1st Edition

0201489295, 978-0201489293

More Books

Students also viewed these Databases questions

Question

6. Discuss the steps involved in conducting a task analysis.

Answered: 1 week ago