def main(): first_try = ['spam', 'cheese', 'mr death'] for i in first_try: try: joke = fun(i) except NameError: continue # 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!') 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'] for i in range(len(langs)): try: more_joke = more_fun(langs[i]) except IndexError: continue last_fun()
""" An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] for entry in first_try: try: joke = fun(entry) except NameError: pass # 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!') 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)
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) joke2 = fun(first_try[1]) # this only runs if there are no exceptions except NameError: print("There is a NameError! 's' is not defined") # Here is a try/except block. Add an else that prints not_joke try: not_joke = fun(first_try[2]) except NameError: print('Run Away!') else: print('not_joke') # this only runs when the exception is not raised # 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,
""" An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) except NameError: try_again = fun(first_try[1]) # 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!') 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)
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) except NameError: print('Whoops! there is no joke for: spam') joke2 = fun(first_try[1]) # 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!') 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)
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) except NameError: print('There is no name!') joke_continue = fun(first_try[1]) # 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!') 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)
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) #first try with first item in list except NameError: joke = fun(first_try[1]) #second try with second item in list else: print(joke) # 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!') 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"
# GiveCleeseHisCheese """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ["spam", "cheese", "mr death"] try: joke = fun(first_try[0]) # pylint:disable=invalid-name except NameError: joke = fun(first_try[1]) # pylint:disable=invalid-name # Here is a try/except block. Add an else that prints not_joke try: not_joke = fun(first_try[2]) # pylint:disable=invalid-name 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)
An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] first_try_idx = 0 try: joke = fun(first_try[first_try_idx]) except NameError: print(f'{first_try[first_try_idx].capitalize()} is no joke!') # 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!') 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)
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) joke = fun(first_try[1]) # intentionally never called except NameError: 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: 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)
#!/usr/bin/python 3 from except_test import fun, more_fun, last_fun first_try = ['spam', 'cheese', 'mr death'] # Exception handles NameError by printing string. try: joke = fun(first_try[0]) except NameError: print("Whoops! There is no joke for: " + first_try[0]) # prints opening dialogue between customer and shopkeeper (lines 1-2) joke = print(fun(first_try[2])) langs = ['java', 'c', 'python'] # prints conclusion question (line 3) try: more_joke = more_fun(langs[0]) except IndexError: more_joke = more_fun(langs[1]) # prints the two lines of the joke (lines 4-5) last_joke = last_fun()
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun first_try = ['spam', 'cheese', 'mr death'] for ft in range(len(first_try)): try: joke = fun(first_try[ft]) # if ft == 2: # print(joke) except NameError: print(f"Whoops! there is no joke for:{first_try[ft]}") langs = ['java', 'c', 'python'] for lang in range(len(langs)): try: if lang == 1: more_joke = more_fun(langs[lang]) except IndexError: print("This Language is not found") finally: last_fun()
""" An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) except NameError: more_joke = fun(first_try[1]) # Something's not right, picking up SyntaxError even at the last commented line. try: not_joke = fun(first_try[2]) except SyntaxError: print("Run Away!") else: print(not_joke) # 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: even_more_joke = more_fun(langs[1]) finally: last_fun()
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) except NameError: print('Whoops! there is no joke for: spam') second_try = fun(first_try[1]) # 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!') else: print(not_joke) # 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,
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: hjoke = fun(first_try[0]) except NameError: print(fun(first_try[2])) # 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 #
Make sure to catch specifically the error you find, rather than all errors. """ # Figure out what the exception is: Line 15 - joke = fun(first_try[0]) # catch it and while still in that catch block, try again with the # second item in the list. # What did that do? : Resolved line 15 error 'Spam' was accepted # 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) from except_test import fun, more_fun, last_fun # import functions from except_test.py file first_try = ['spam', 'cheese', 'mr death'] # list values try: # attempt this, on error, go to except block joke = fun(first_try[0]) # first value 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
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) except NameError: print("There is no joke for {} you scurvy fool!".format(first_try[0])) else: print("this failed...") # 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!') 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"
#!/usr/bin/python """ An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list first_try = ['spam', 'cheese', 'mr death'] try: joke = fun(first_try[0]) except NameError: print(f"{first_try[0]} is not a valid joke.") # 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!') 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
""" An exercise in playing with Exceptions. Make lots of try/except blocks for fun and profit. Make sure to catch specifically the error you find, rather than all errors. """ from except_test import fun, more_fun, last_fun # Figure out what the exception is, catch it and while still # in that catch block, try again with the second item in the list # This has the desired outcome using a for loop and continue first_try = ['spam', 'cheese', 'mr death'] for i in first_try: try: joke = fun(i) if joke is not None: print(joke) except NameError: continue # 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)