# this file available at: https://arachnoid.com/files/python01/ import math from typing import List def student_error_00(): print(f'\tLoop numeric float test: detect zero.') n = 0.3 while n >= -0.31: print(f'\tn = {n:18.15f} or {n:22.15e}, equals zero? {n == 0.0}') # test for zero if n == 0.0: break else: n -= .1 def student_error_01_append_test(mylist :List[str] = []): # a function default value is only applied # once, when the function is defined, not # each time the function is called. mylist.append(f"append {len(mylist)}") print(f'\t{mylist}') def student_error_01(): print('\tAppend to empty list:') for _ in range(5): student_error_01_append_test() def student_error_02(): try: _ = math.log(0) except Exception as e: print(f'\tCaught error = {e}') print('\t... function continues from here.') def student_error_03(): try: x = math.log(0) print(f'\tx = {x}') except Exception as err: print(f'\tCaught outer error: {err}') try: y = 1/0 print(f'\tx = {y}') except Exception as err: print(f'\tCaught inner error: {err}') print('\t... function continues from here.') examples = ( student_error_00, student_error_01, student_error_02, student_error_03, ) def main(): for example in examples: print(f'Running {example.__name__}:') example() print() if __name__ == "__main__": main()