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) try: langs = ['java', 'c', 'python'] more_joke = more_fun(langs[0]) last_fun() except IndexError: more_joke = more_fun(langs[1]) last_fun() else: more_fun(langs[-1]) last_fun()
# 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'}
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]) # more_fun(langs[n]) returns nothing except IndexError: pass # print("\nWhoops! index 5 is way out of index in the list [1, 2, 3]\n") finally: # since above IndexError occurred, to continue using the same try - except block # finally:, what is executed disregarding whatever else happend has to be used more_joke = more_fun(langs[1]) # more_fun(langs[n]) returns nothing last_fun()
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]) finally: lastItem = len(langs) - 1 more_fun(langs[lastItem]) last_fun()
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()
try: 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 next_joke. # # If there are no exceptions, call the more_fun function with the last # language in the list regardless of whether there was an exception # 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: next_joke = more_fun(langs[1]) last_fun()
# in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] joke = fun(first_try[0]) # Here is a try/except block. Add an else that prints not_joke try: not_joke = fun(first_try[2]) 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'] more_joke = more_fun(langs[0])
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)