import os from IPython.display import Markdown as md report = False def consent(): """ Ask for consent the first time, and remember this by storing a dot file """ fname = os.path.expanduser("~/.phys105_report") if os.path.isfile(fname): with open(".report", "r") as f: report = bool(int(f.read().strip("\n"))) return report text = "Do you agree to consent? " pos = ["y", "yes"] neg = ["n", "no"] answer = input(text).lower() while answer not in pos + neg: answer = input("Please input Yes (Y) or No (N) ").lower() with open(fname, "w") as f: if answer in pos: f.write("1") return True else: f.write("0") return False return def custom_exc(shell, etype, evalue, tb, tb_offset=None): final = """ If you are still unsure, please ask one of the demonstrators""" shell.showtraceback((etype, evalue, tb), tb_offset=tb_offset) if etype == ModuleNotFoundError: display(md( """ ## ModuleNotFoundError Python is telling you it cannot find the module you tried to import. What to do: take a look at the line number indicated by the error message, near where the ^ points and * Check that the you spelt the module name correctly * If so, check that the package is installed on your system. """ + final)) elif etype == SyntaxError: display(md( """ ## SyntaxError Python is telling you that what you have written is not valid python code. What to do: take a look at the line number indicated by the error message, near where the ^ points (or the end of the pervious line if it points to the start of a line) and check * If you are missing an opening or closing bracket/brace * If you have too many openning or closing brackets/braces * if you are missing a comma or have one in an unexpected place """ + final)) elif etype == IndentationError: display(md( """ ## IndentationError Python is telling you that it has found an inconsistent level of indentation. This could be e.g. in a function defintion, loop or conditional statement. What to do: take a look at the line number indicated by the error message, near where the ^ points and check * That the Identation is all of the same amount (i.e. same number of spaces or tabs) """ + final)) elif etype == TabError: display(md( """ ## TabError Python is telling you that it has found a mix of spaces and tabs used to denote indentation. This could be e.g. in a function defintion, loop or conditional statement. What to do: take a look at the line number indicated by the error message, near where the ^ points and * Try deleting the indentation and replacing it with either spaces or tabs in each case, not a mix. """ + final)) elif etype == NameError: display(md( """ ## NameError Python is telling you that the variable, function or module you are trying to use cannot be found. What to do: take a look at the line number indicated by the error message, near where the ^ points (or the end of the pervious line if it points to the start of a line) and check * You have spelt the name of the variable/function correctly * You have imported the necessary module * That you have run the cell that has the defintion or import """ + final)) elif etype == TypeError: display(md( """ ## TypeError Python is telling you that you are trying to do something with the wrong type of variable or combine/compare variables that are of incompatible type What to do: take a look at the line number indicated by the error message, near where the ^ points * Make sure you have convereted a number from a string to an int/float if you are trying to do mathematical calculations * Print the type() of the variable and check it is what you expect """ + final)) elif etype == ValueError: display(md( """ ## ValueError Python is telling you that the variable you are using has the correct type but that the value is not acceptable. What to do: take a look at the line number indicated by the error message, near where the ^ points and check: * If you are using a negative value where a positive one is requiered (e.g. in sqrt) """ + final)) elif etype == AttributeError: display(md( """ ## AttributeError Python is telling you that you are asking an object or module for a variable or function it doesn't provide What to do: take a look at the line number indicated by the error message, near where the ^ points and: * Check that the variable or function is spelt correctly * Print the type() of the variable and check it is what you expect """ + final)) elif etype == KeyError: display(md( """ ## KeyError Python is telling you that you are trying to access an element of a dictionary using a key that doesn't exist What to do: take a look at the line number indicated by the error message, near where the ^ points and: * Check if the key is spelt correctly * Print out the dict to see what it contains """ + final)) elif etype == IndexError: display(md( """ ## IndexError Python is telling you that you are trying to access an element of a data structure (e.g. list/tuple/string/array etc) that is bigger than the size of the structure What to do: take a look at the line number indicated by the error message, near where the ^ points and: * If you are using a variable as the index, print it out and make sure the value is what you expect * Print out the len() of the data structure * Make sure that you are remember that indices in python start from 0 and go to size - 1 """ + final)) elif etype == ZeroDivisionError: display(md( """ ## ZeroDivisionError: Python is telling you that you are trying to divide a number by zero. What to do: take a look at the line number indicated by the error message, near where the ^ points and: * Check if the denominator is unexpectedley zero * Use a conditional statement to check the denominator is not zero before performing the division """ + final)) else: display(md( """ We have not implemented an explanation for this error. Try * Looking at the python error documentation: https://docs.python.org/3/library/exceptions.html * Try googling the error """ + final)) return report = consent() del consent # this registers a custom exception handler for the whole current notebook get_ipython().set_custom_exc((Exception,), custom_exc) del custom_exc