# What did that do? You can think of else in this context, as well as in # loops as meaning: "else if nothing went wrong" # (no breaks in loops, no exceptions in try blocks) # Figure out what the exception is, catch it and in that same block # # try calling the more_fun function with the 2nd language in the list, # again assigning it to more_joke. # # If there are no exceptions, call the more_fun function with the last # language in the list # Finally, while still in the try/except block and regardless of whether # there were any exceptions, call the function last_fun with no # parameters. (pun intended) langs = ['java', 'c', 'python'] try: more_joke = more_fun(langs[1]) except SyntaxError: pass except IndexError: pass else: more_fun(langs[2]) finally: last_fun() comprehension = {c for c in 'aabbbcccc'}
except Exception as e: print('Whoops! There is no joke for: {}'.format(first_try[0])) # Here is a try/except block. Add an else that prints not_joke try: # check last element of first_try list not_joke = fun(first_try[2]) except Exception as e: print('Run Away!', e) else: print(not_joke) # if not error, print result # Figure out what the exception is, catch it and in that same block # try calling the more_fun function with the 2nd language in the list, # again assigning it to more_joke. # If there are no exceptions, call the more_fun function with the last # language in the list # Finally, while still in the try/except block and regardless of whether # there were any exceptions, call the function last_fun with no # parameters. (pun intended) langs = ['java', 'c', 'python'] # new list try: # more_joke = more_fun(langs[0]) # test list val 1 more_joke = more_fun(langs[1]) # test list val 2 more_joke = more_fun(langs[2]) # test list val 3 final_joke = last_fun() # test list without indicating value except Exception as e: print('Oh no!', e)
not_joke = fun(first_try[2]) except SyntaxError: print('Run Away!') else: print(not_joke) # What did that do? You can think of else in this context, as well as in # loops as meaning: "else if nothing went wrong" # (no breaks in loops, no exceptions in try blocks) # Figure out what the exception is, catch it and in that same block # try calling the more_fun function with the 2nd language in the list, # again assigning it to more_joke. # # If there are no exceptions, call the more_fun function with the last # language in the list # Finally, while still in the try/except block and regardless of whether # there were any exceptions, call the function last_fun with no # parameters. (pun intended) langs = ['java', 'c', 'python'] try: more_joke = more_fun(langs[0]) except IndexError: more_joke = more_fun(langs[1]) pythonlanguage = more_fun(langs[-1:]) finalrun = last_fun()
except SyntaxError: print('Run Away!') # What did that do? You can think of else in this context, as well as in # loops as meaning: "else if nothing went wrong" # (no breaks in loops, no exceptions in try blocks) # Figure out what the exception is, catch it and in that same block # # try calling the more_fun function with the 2nd language in the list, # again assigning it to more_joke. # # If there are no exceptions, call the more_fun function with the last # language in the list # Finally, while still in the try/except block and regardless of whether # there were any exceptions, call the function last_fun with no # parameters. (pun intended) langs = ['java', 'c', 'python'] try: more_joke = more_fun(langs[0]) except IndexError: print("Caught index error") more_joke = more_fun(langs[1]) else: more_joke = more_fun(langs[-1]) finally: more_joke = last_fun()