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

b) It is often useful to anticipate what kinds of errors can occur in a piece of

ID: 3809925 • Letter: B

Question

b) It is often useful to anticipate what kinds of errors can occur in a piece of code. Name at least four exceptions that could occur from the following lines of code, and identify which line it could occur in. You *can* consider the possibility that new_mod or random string or m is not defined, but you should assume that if new_mod and random_string are defined then they won't any errors by themselves. (line 1) >>> import new_mod (line 2) >>> ranstr = newmod.random_string () (line 3) >>> assert type(ranstr) == str (line 4) >>> n = len(ranstr) % m

Explanation / Answer

import new_mod
ranstr = newmod.random_string()
assert type(ranstr) == str
n = len(ranstr) % m

------------------------------------------------------------------------------------------------------------------------------------
****** HERE ARE LIST OF ERRORS CAN OCCUR ***********
1st error at line number 1: ImportError: No module named new_mod
2nd error at line number 2: random_string method found
3rd error at line number 3: ranstr was undefined
4th error at line number 4: variable m not declared

---------------------------------------------------------------------------------------------------------------------------------------
Explanation:
-----------------
1st error can occur since if module missed, then it might cause error
2nd error can occur if method is missing in newmod class, then it will return error and ranstr will contain undefined value
3rd error will occur if in the 2nd line random_string() method not found or return undefined value.
4th error is straight way... anywhere m value is not defined, so it will cause error.

---------------------------------------------------------------------------------------------------------------------------------------