1
2
3
4
5
6
7
8
9
10
11
12
To practice on your own, or to check code you believe shouldn't have been scored as incorrect, go to Trinket.
Post a review on Amazon for the paperback or the Kindle edition.
Email me to give me a compliment, complaint, or correction. I'll respond.
0,1,2,3,4,5,6,7,8,9,10,11
0
0
0
0
Fill in the blank. | ______ filename = input("What text file to open? ") with open(filename) as f: print(f.read()) except FileNotFoundError: print("Sorry, " + filename + " not found.") |
try: | ^ *try: *$ |
Fill in the blank. | try: filename = input("What text file to open? ") with open(filename) as f: print(f.read()) _______ FileNotFoundError: print("Sorry, " + filename + " not found.") |
except | ^ *except *$ |
Fill in the blank | try: filename = input("What text file to open? ") with open(filename) as f: print(f.read()) except __________________ print("Sorry, " + filename + " not found.") | FileNotFoundError: | File`Not`Found`Error`: |
Fill in the blank. | try: filename = input("What text file to open? ") with open(filename) as f: print(f.read()) ________________________ print("Sorry, " + filename + " not found.") | except FileNotFoundError: | except FileNotFoundError: |
Code the first two lines of a block with exception handling. The second line opens a JSON file for writing. Assume the json module has already been imported. Make up what you need to. | try: with open("xyz.json", "w") as j: |
^ *try:\n with open\(•.+\.json•, •w•\) as [a-zA-Z_][a-zA-Z0-9_]*: *$ | |
Code the next line to deal with the file not being found. | try: with open("xyz.json", "w") as j: |
except FileNotFoundError: | ^ *except FileNotFoundError: *$ |
Code the next line. It displays a message. Make up the message. Remember to indent. | try: with open("xyz.json", "w") as j: json.dump(some_list, j) except FileNotFoundError: |
print("Oh no!") | ^ * print\(•.+•\) *$ |
Code the first two lines of a block with exception handling. The second line opens a text file for reading. Use shorthand. Make up what you need to. | try: with open("xyz.txt") as f: |
^ *try:\n with open\(•.+\.txt•\) as [a-zA-Z_][a-zA-Z0-9_]*: *$ | |
Code the next line to display a message. The message is stored in a variable. Make up the variable name. Remember to indent. | except: | print(error_message) | ^ * print\([a-zA-Z_][a-zA-Z0-9_]*\) *$ |
Code five lines that attempt to open a JSON file and read it. If the file doesn't open, display a message. Use shorthand. Make up what you need to. Assume that the json module has already been imported. The third line begins with a double indent. | try: with open("doctors.json") as j: j_content = json.load(j) except FileNotFoundError: print("The file cannot be found.") |
^ *try:\n with open\(•.+\.json•\) as ([a-zA-Z_][a-zA-Z0-9_]*):\n [a-zA-Z_][a-zA-Z0-9_]* = json\.load\(\1\)\nexcept FileNotFoundError:\n print\(•.+•\) *$ | |
|
|||