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):
-
input1 should be a list
-
All of the values in input1 should be numeric. It is ok if input1 is empty
-
If there are multiple non-numeric values, you only need to throw an exception for the first non-numeric value encountered
-
-
input2 should be numeric
-
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):
-
filepath should be a string
-
filepath should be a valid filepath (i.e. we can open the file)
-
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started