###### exception ###### print('---------- exception 1 --------------') # First run, try entering 3 in the console # Notice pop(value) looks for key '3' (input() results in string) # in the dict, return the value associated with the key, # and remove that key-value pair from the dict. # The appearance of console shows # {0: 'cat', 3: 'dog'} -- why? # Now, let's run this exception 1 again and enter 5 in the console # Observe the appearance of console shows # key is not found! -- why? x = input('give me a key to remove item from my dict : ') try: d = {} d[0] = "cat" d[3] = "dog" d["3"] = "bird" d.pop(x) print(d) except ZeroDivisionError: print('divide by zero') except TypeError: print("type mismatch!") except IndexError: print("index out of bound!") except KeyError: print("key is not found!") except: print("caught") ########################### print('---------- exception 2 --------------') x = input('give me a denominator : ') # try enter 0 in the console try: d = {} d[0] = "cat" d[3] = "dog" d["3"] = "bird" d['4'] = 5 print(d['4'] / int(x)) except ZeroDivisionError: print('divide by zero') except TypeError: print("type mismatch!") except IndexError: print("index out of bound!") except KeyError: print("key is not found!") except: print("caught") ########################### print('---------- exception 3 --------------') x = input('give me a denominator : ') # try enter some number try: d = {} d[0] = "cat" d[3] = "dog" d["3"] = "bird" d['4'] = '5' print(d['4'] / int(x)) except ZeroDivisionError: print('divide by zero') except TypeError: print("type mismatch!") except IndexError: print("index out of bound!") except KeyError: print("key is not found!") except: print("caught")